selectRandomWeighted: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "<dl class="command_description"> <dd class="notedate">" to "<dl class="command_description"> <dt></dt> <dd class="notedate">")
m (Some wiki formatting)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


|arma3
|game1= arma3
|1.76
|version1= 1.76


|gr1= Arrays
|gr1= Arrays


|Returns a random element from the given array according to assigned weights. Engine solution to [[BIS_fnc_selectRandomWeighted]]. A few notes:
|descr= 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
* Negative weights are not supported
* If an item's weight is 0, the item is ignored
* If an item's weight is 0, the item is ignored
* The weights don't have to total to 1  
* The weights don't have to total to 1  
* 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
* A single array Syntax is slightly faster than Alt Syntax


|'''selectRandomWeighted''' valuesAndWeights
|s1= [[selectRandomWeighted]] valuesAndWeights


|p1= valuesAndWeights: [[Array]] - array in format [value1, weight1, value2, weight2...], where:<span>
|p1= valuesAndWeights: [[Array]] - array in format [value1, weight1, value2, weight2, ...], where:
* value: [[Anything]] - weighted value
* value: [[Anything]] - weighted value
* weight: [[Number]] - values relative weight
* weight: [[Number]] - values relative weight
</span>


| [[Anything]] - random weighted value or [[Nothing]] if array is empty or all weights are 0
|r1= [[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]] of [[Anything]]
|p22= weights: [[Array]] - array of [[Number]]s
 
|p22= weights: [[Array]] of [[Number]]s


|r2= [[Anything]] - random weighted value or [[Nothing]] if any of arrays is empty or all weights are 0
|r2= [[Anything]] - random weighted value or [[Nothing]] if any of arrays is empty or all weights are 0
 
|x1= <code>_randomWeightedElement = [[selectRandomWeighted]] [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];</code>
|x2= <code>_randomWeightedElement = [1,2,3,4,5] [[selectRandomWeighted]] [0.1,0.2,1.3,2.4,7.5];</code>


|seealso= [[random]], [[select]], [[selectRandom]], [[selectMin]], [[selectMax]]
|x1= <sqf>private _randomWeightedElement = selectRandomWeighted [1,0.1,2,0.2,3,1.3,4,2.4,5,7.5];</sqf>
 
|x2= <sqf>private _randomWeightedElement = [1,2,3,4,5] selectRandomWeighted [0.1,0.2,1.3,2.4,7.5];</sqf>
 
|seealso= [[random]] [[select]] [[selectRandom]] [[selectMin]] [[selectMax]]
}}
}}
<!-- CONTINUE Notes -->
 
<dl class="command_description">
{{Note
<dt></dt>
|user= Killzone_Kid
<dd class="notedate">Posted on June 26, 2017 - 19:17 (UTC)</dd>
|timestamp= 20170626191700
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
|text= A 100000 runs of the following code:
<dd class="note">A 100000 runs of the following code:
<sqf>
<code>_values = [0,1,2,3,4,5,6,7,8,9];
_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];
_weights = [0.109,0.65,0.01,1.01,1.24,0,1.59,0.09,1.15,0.55];
_result = _values [[selectRandomWeighted]] _weights;</code>
_result = _values selectRandomWeighted _weights;
</sqf>
Gave this result:
Gave this result:
<pre>0 - 0
<pre>
0 - 0
0.01 - 144
0.01 - 144
0.09 - 1409
0.09 - 1409
Line 53: Line 56:
1.15 - 18059
1.15 - 18059
1.24 - 19518
1.24 - 19518
1.59 - 24769 </pre>
1.59 - 24769
</dd>
</pre>
</dl>
}}
<!-- DISCONTINUE Notes -->

Latest revision as of 12:54, 6 May 2022

Hover & click on the images for description

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 - array in format [value1, weight1, value2, weight2, ...], where:
  • value: Anything - weighted value
  • weight: Number - values relative weight
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:
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];

Additional Information

See also:
random select selectRandom selectMin selectMax

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
Killzone_Kid - c
Posted on Jun 26, 2017 - 19:17 (UTC)
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