selectRandomWeighted: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Text replacement - "\{\{GameCategory\|(arma[0123])\|[ _]?New[ _]Scripting[ _]Commands[ _]List\}\}" to "{{GameCategory|$1|New Scripting Commands}}") |
Lou Montana (talk | contribs) m (Text replacement - "_{10,} " to "") |
||
Line 1: | Line 1: | ||
{{Command|Comments= | {{Command|Comments= | ||
|arma3|Game name= | |arma3|Game name= | ||
Line 6: | Line 5: | ||
|gr1= Arrays |GROUP1= | |gr1= Arrays |GROUP1= | ||
|Returns a random element from the given array according to assigned weights. Engine solution to [[BIS_fnc_selectRandomWeighted]]. A few notes: | |Returns a random element from the given array according to assigned weights. Engine solution to [[BIS_fnc_selectRandomWeighted]]. A few notes: | ||
Line 14: | Line 12: | ||
* If numbers of items and weights do not match, the shortest array is used | * If numbers of items and weights do not match, the shortest array is used | ||
* A single array Syntax is slightly faster then Alt Syntax |DESCRIPTION= | * A single array Syntax is slightly faster then Alt Syntax |DESCRIPTION= | ||
|'''selectRandomWeighted''' valuesAndWeights |SYNTAX= | |'''selectRandomWeighted''' valuesAndWeights |SYNTAX= | ||
Line 31: | Line 28: | ||
|r2= [[Anything]] - random weighted value or [[Nothing]] if any of arrays is empty or all weights are 0 |RETURNVALUE2= | |r2= [[Anything]] - random weighted value or [[Nothing]] if any of arrays is empty or all weights are 0 |RETURNVALUE2= | ||
|x1= <code>_randomWeightedElement = [[selectRandomWeighted]] [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];</code> |EXAMPLE1= | |x1= <code>_randomWeightedElement = [[selectRandomWeighted]] [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];</code> |EXAMPLE1= | ||
|x2= <code>_randomWeightedElement = [1,2,3,4,5] [[selectRandomWeighted]] [0.1,0.2,1.3,2.4,7.5];</code> |EXAMPLE2= | |x2= <code>_randomWeightedElement = [1,2,3,4,5] [[selectRandomWeighted]] [0.1,0.2,1.3,2.4,7.5];</code> |EXAMPLE2= | ||
| [[random]], [[select]], [[selectRandom]], [[selectMin]], [[selectMax]] |SEEALSO= | | [[random]], [[select]], [[selectRandom]], [[selectMin]], [[selectMax]] |SEEALSO= |
Revision as of 02:27, 17 January 2021
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 then Alt Syntax
- Groups:
- Arrays
Syntax
- Syntax:
- selectRandomWeighted valuesAndWeights
- Parameters:
- valuesAndWeights: Array - array in format [value1, weight1, value2, weight2...], where:
- 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 - array of Anything
- weights: Array - 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:
_randomWeightedElement = selectRandomWeighted [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];
- Example 2:
_randomWeightedElement = [1,2,3,4,5] selectRandomWeighted [0.1,0.2,1.3,2.4,7.5];
Additional Information
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
- Posted on June 26, 2017 - 19:17 (UTC)
- Killzone Kid
- A 100000 runs of the following code:
_values = [0,1,2,3,4,5,6,7,8,9]; _weights = [0.109,0.65,0.01,1.01,1.24,0,1.59,0.09,1.15,0.55]; _result = _values selectRandomWeighted _weights;
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