diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index c7856c652..b9ea0c675 100644 --- a/addons/arrays/CfgFunctions.hpp +++ b/addons/arrays/CfgFunctions.hpp @@ -15,6 +15,7 @@ class CfgFunctions { PATHTO_FNC(join); PATHTO_FNC(reject); PATHTO_FNC(select); + PATHTO_FNC(selectRandomArray); PATHTO_FNC(shuffle); PATHTO_FNC(sortNestedArray); }; diff --git a/addons/arrays/fnc_selectRandomArray.sqf b/addons/arrays/fnc_selectRandomArray.sqf new file mode 100644 index 000000000..d9abe1666 --- /dev/null +++ b/addons/arrays/fnc_selectRandomArray.sqf @@ -0,0 +1,37 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_selectRandomArray + +Description: + Select a specified amount of elements from an array without picking the same element multiple times. + +Parameters: + _array - Input Array + _amount - Amount to select + +Returns: + New array with the specified amount of randomly selected elements + +Example: + (begin example) + _result = [[1, 2, 3, 4, 5], 2] call CBA_fnc_selectRandomArray; + // _result => [2, 4] (random) + (end) + +Author: + NeilZar +---------------------------------------------------------------------------- */ +SCRIPT(selectRandomArray); + +params [["_array", [], [[]]], ["_amount", 0, [0]]]; + +_amount = _amount min count _array; +_array = + _array; + +private _result = []; + +for "_i" from 1 to _amount do { + _result pushBack (_array deleteAt floor random count _array); +}; + +_result