BIS fnc selectRandomWeighted: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (example added)
m (Some wiki formatting)
 
(39 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{RV|type=function


{{Function|= Comments
|game1= arma2
____________________________________________________________________________________________
|version1= 1.00


| arma2 |= Game name
|game2= arma2oa
|version2= 1.50


|1.00|= Game version
|game3= tkoh
____________________________________________________________________________________________
|version3= 1.00


| <pre>/*
|game4= arma3
|version4= 0.50


Description:
|gr1= Arrays
Function to select a random item from an array, taking into account item weights.
 
The weights should be Numbers between 0 and 1, with a maximum precision of hundreds.
|descr= Selects a random item from an array, taking into account item weight.
{{Feature|informative|
Parameter(s):
* The weights do not have to total to 1
_this select 0: source Array (Array of Any Value)
* The length of weights and items arrays may not match, in which case the shortest array is used for length
_this select 1: weights (Array of Number)
}}
Returns:
Any Value selected item
TODO:
[*] Algorithm is inefficient?
*/


</pre><small>''(Placeholder description extracted from the function header by [[BIS_fnc_exportFunctionsToWiki]])''</small> |= Description
{{Feature|arma3|See [[selectRandomWeighted]] for an engine solution.}}
____________________________________________________________________________________________


| [array1, array2] call BIS_fnc_selectRandomWeighted; |= Syntax
|s1= itemsAndWeights call [[BIS_fnc_selectRandomWeighted]]


|p1=array1: [[Array]] - selectable values |= Parameter 1
|p1= itemsAndWeights: [[Array]] - array of items and weights [item, weight, item, weight...]
|p2=array2: [[Array]] of [[Number]]s - values weight: first weight is for array1 first element, etc. |= Parameter 1


| |= Return value
|r1= [[Anything]]
____________________________________________________________________________________________


|x1= <code>_choices = ["cat", "dog"];
|s2= [items, weights] call [[BIS_fnc_selectRandomWeighted]]
_weights = [0.5, 1.0];
[_choices, _weights] call BIS_fnc_selectRandomWeighted]; // will result in 2/3 of dogs, 1/3 of cats</code> |=
|p21= items: [[Array]] - items array of [[Anything]]
____________________________________________________________________________________________


| |= See also
|p22= weights: [[Array]] - weights array of [[Number]]s


}}
|r2= [[Anything]]


<h3 style="display:none">Notes</h3>
|x1= <sqf>["apples",.3,"pears",.2,"bananas",.4,"diamonds",.1,"unicorns",.00001] call BIS_fnc_selectRandomWeighted;</sqf>
<dl class="command_description">
<!-- Note Section BEGIN -->


<!-- Note Section END -->
|x2= <sqf>[["apples","pears","bananas","diamonds"], [0.3,0.2,0.4,0.1]] call BIS_fnc_selectRandomWeighted;</sqf>
</dl>


<h3 style="display:none">Bottom Section</h3>
|seealso= [[selectRandomWeighted]]
[[Category:Function Group: Arrays|{{uc:selectRandomWeighted}}]]
}}
[[Category:Functions|{{uc:selectRandomWeighted}}]]
[[Category:{{Name|arma2}}: Functions|{{uc:selectRandomWeighted}}]]
[[Category:{{Name|arma2oa}}: Functions|{{uc:selectRandomWeighted}}]]
[[Category:{{Name|tkoh}}: Functions|{{uc:selectRandomWeighted}}]]
[[Category:{{Name|arma3}}: Functions|{{uc:selectRandomWeighted}}]]

Latest revision as of 16:41, 13 July 2022

Hover & click on the images for description

Description

Description:
Selects a random item from an array, taking into account item weight.
  • The weights do not have to total to 1
  • The length of weights and items arrays may not match, in which case the shortest array is used for length
Arma 3
See selectRandomWeighted for an engine solution.
Execution:
call
Groups:
Arrays

Syntax

Syntax:
itemsAndWeights call BIS_fnc_selectRandomWeighted
Parameters:
itemsAndWeights: Array - array of items and weights [item, weight, item, weight...]
Return Value:
Anything

Alternative Syntax

Syntax:
[items, weights] call BIS_fnc_selectRandomWeighted
Parameters:
items: Array - items array of Anything
weights: Array - weights array of Numbers
Return Value:
Anything

Examples

Example 1:
["apples",.3,"pears",.2,"bananas",.4,"diamonds",.1,"unicorns",.00001] call BIS_fnc_selectRandomWeighted;
Example 2:
[["apples","pears","bananas","diamonds"], [0.3,0.2,0.4,0.1]] call BIS_fnc_selectRandomWeighted;

Additional Information

See also:
selectRandomWeighted

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