BIS fnc selectRandomWeighted: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(see also)
(docs updated)
Line 8: Line 8:
____________________________________________________________________________________________
____________________________________________________________________________________________


| <pre>/*
| Selects a random item from an array, taking into account item weight. Note:
* The weights don't have to total to 1
* The length of weights and items arrays may not match, in which case the shortest array is used for length|= Description
____________________________________________________________________________________________


Description:
| itemsAndWeights call [[BIS_fnc_selectRandomWeighted]] |= Syntax
Function to select a random item from an array, taking into account item weights.
The weights should be Numbers between 0 and 1, with a maximum precision of hundreds.
Parameter(s):
_this select 0: source Array (Array of Any Value)
_this select 1: weights (Array of Number)
Returns:
Any Value selected item
TODO:
[*] Algorithm is inefficient?
*/


</pre><small>''(Placeholder description extracted from the function header by [[BIS_fnc_exportFunctionsToWiki]])''</small> |= Description
|p1=itemsAndWeights: [[Array]] - array of items and weights [item, weight, item, weight...]|= Parameter 1
____________________________________________________________________________________________


| [array1, array2] call BIS_fnc_selectRandomWeighted; |= Syntax
| [[Anything]] |= Return value


|p1=array1: [[Array]] - selectable values |= Parameter 1
|s2= [items, weights] call [[BIS_fnc_selectRandomWeighted]] |= Syntax
|p2=array2: [[Array]] of [[Number]]s - values weight: first weight is for array1 first element, etc. |= Parameter 1
|p21=[items, weights]: [[Array]]|= Parameter 1
|p22=items: [[Array]] - items array of [[Anything]] |= Parameter 1
|p23=weights: [[Array]] - weights array of [[Number]]s |= Parameter 1


| |= Return value
|r2= [[Anything]] |= Return value
____________________________________________________________________________________________
____________________________________________________________________________________________
 
|x1= <code>["apples",.3,"pears",.2,"bananas",.4,"diamonds",.1,"unicorns",.00001] [[call]] [[BIS_fnc_selectRandomWeighted]];</code> |=
|x1= <code>_choices = ["cat", "dog"];
|x2= <code><nowiki>[</nowiki>["apples","pears","bananas","diamonds"],[0.3,0.2,0.4,0.1]] [[call]] [[BIS_fnc_selectRandomWeighted]];</code> |=  
_weights = [0.5, 1.0];
[_choices, _weights] call BIS_fnc_selectRandomWeighted]; // will result in 2/3 of dogs, 1/3 of cats</code> |=  
____________________________________________________________________________________________
____________________________________________________________________________________________



Revision as of 22:00, 26 June 2017


Hover & click on the images for description

Description

Description:
Selects a random item from an array, taking into account item weight. Note:
  • The weights don't have to total to 1
  • The length of weights and items arrays may not match, in which case the shortest array is used for length
Execution:
call
Groups:
Uncategorised

Syntax

Syntax:
itemsAndWeights call BIS_fnc_selectRandomWeighted
Parameters:
itemsAndWeights: Array - array of items and weights [item, weight, item, weight...]
Return Value:
Anything

Alternative Syntax

Syntax:
[items, weights] call BIS_fnc_selectRandomWeighted
Parameters:
[items, weights]: Array
items: Array - items array of Anything
weights: Array - weights array of Numbers
Return Value:
Anything

Examples

Example 1:
["apples",.3,"pears",.2,"bananas",.4,"diamonds",.1,"unicorns",.00001] call BIS_fnc_selectRandomWeighted;
Example 2:
[["apples","pears","bananas","diamonds"],[0.3,0.2,0.4,0.1]] call BIS_fnc_selectRandomWeighted;

Additional Information

See also:
selectRandomWeighted

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note

Notes

Bottom Section