selectRandomWeighted: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - " " to " ")
m (Text replacement - " \|(x[0-9]|p[0-9]{1,2}|descr|game[0-9]|version[0-9]|s[0-9]|exec|r[0-9]|arg|eff|branch|serverExec|gr[0-9]|mp|pr|seealso) +=" to " |$1=")
Line 22: Line 22:
| [[Anything]] - random weighted value or [[Nothing]] if array is empty or all weights are 0
| [[Anything]] - random weighted value or [[Nothing]] if array is empty or all weights are 0


|s2 = values '''selectRandomWeighted''' weights
|s2= values '''selectRandomWeighted''' weights


|p21= values: [[Array]] - array of [[Anything]]
|p21= values: [[Array]] - array of [[Anything]]

Revision as of 23:30, 10 June 2021

Hover & click on the images for description

Description

Description:
Description needed
Groups:
Arrays

Syntax

Syntax:
Syntax needed
Parameters:
valuesAndWeights: Array - array in format [value1, weight1, value2, weight2...], where:
  • value: Anything - weighted value
  • weight: Number - values relative weight
Return Value:
Return value needed

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

See also:
randomselectselectRandomselectMinselectMax

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