Arma 3 Scripting

Command in Arrays

selectRandomWeighted

Also available as Markdown and JSON, or read the original wiki page.

Description

Returns a random element from the given array according to assigned weights. Engine solution to BIS_fnc_selectRandomWeighted. A few notes:

Syntax

Syntax 1

selectRandomWeighted valuesAndWeights

Parameters:

Returns: Anything - random weighted value or Nothing if array is empty or all weights are 0

Syntax 2

values selectRandomWeighted weights

Parameters:

Returns: Anything - random weighted value or Nothing if any of arrays is empty or all weights are 0

Examples

Example 1

private _randomWeightedElement = selectRandomWeighted [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];

Example 2

private _randomWeightedElement = [1,2,3,4,5] selectRandomWeighted [0.1,0.2,1.3,2.4,7.5];

See also

random · select · selectRandom · selectMin · selectMax

Community notes

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