selectRandomWeighted: Difference between revisions
BrettMayson (talk | contribs) mNo edit summary |
BrettMayson (talk | contribs) mNo edit summary |
||
| Line 15: | Line 15: | ||
|s1= [[selectRandomWeighted]] valuesAndWeights | |s1= [[selectRandomWeighted]] valuesAndWeights | ||
|p1= valuesAndWeights: [[Array]] with [valueN, weightN] | |p1= valuesAndWeights: [[Array]] with [valueN, weightN] | ||
* valueN: [[Anything]] - weighted value | * valueN: [[Anything]] - weighted value | ||
* weightN: [[Number]] - values relative weight | * weightN: [[Number]] - values relative weight | ||
Latest revision as of 10:02, 2 January 2026
Description
- Description:
- Returns a random element from the given array according to assigned weights. Engine solution to BIS_fnc_selectRandomWeighted. A few notes:
- Negative weights are not supported
- If an item's weight is 0, the item is ignored
- The weights don't have to total to 1
- If numbers of items and weights do not match, the shortest array is used
- A single array Syntax is slightly faster than Alt Syntax
- Groups:
- Arrays
Syntax
- Syntax:
- selectRandomWeighted valuesAndWeights
- Parameters:
- valuesAndWeights: Array with [valueN, weightN]
- Return Value:
- Anything - random weighted value or Nothing if array is empty or all weights are 0
Alternative Syntax
- Syntax:
- values selectRandomWeighted weights
- Parameters:
- values: Array of Anything
- weights: Array of Numbers
- Return Value:
- Anything - random weighted value or Nothing if any of arrays is empty or all weights are 0
Examples
- Example 1:
- Example 2:
Additional Information
- See also:
- random select selectRandom selectMin selectMax
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord.
Only post proven facts here! Add Note
- Posted on Jun 26, 2017 - 19:17 (UTC)
-
A 100000 runs of the following code:
Gave this result:
0 - 0 0.01 - 144 0.09 - 1409 0.109 - 1633 0.55 - 8673 0.65 - 10127 1.01 - 15668 1.15 - 18059 1.24 - 19518 1.59 - 24769