Skip to content

Commit

Permalink
add CBA_fnc_selectRandomArray (#1049)
Browse files Browse the repository at this point in the history
* Initial commit

* Fixes and improvements

* Update addons/arrays/fnc_selectRandom.sqf

Co-Authored-By: neilzar <neil.evers.1995@gmail.com>

* Implement reviews

* I want a min there

* Add semicolon to please OCD

Co-Authored-By: neilzar <neil.evers.1995@gmail.com>

* Rename to selectSomeRandom

* function name

* update function name

* update function name
  • Loading branch information
neilzar authored and commy2 committed Feb 9, 2019
1 parent 70fbbb5 commit d38acc9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions addons/arrays/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CfgFunctions {
PATHTO_FNC(join);
PATHTO_FNC(reject);
PATHTO_FNC(select);
PATHTO_FNC(selectRandomArray);
PATHTO_FNC(shuffle);
PATHTO_FNC(sortNestedArray);
};
Expand Down
37 changes: 37 additions & 0 deletions addons/arrays/fnc_selectRandomArray.sqf
Original file line number Diff line number Diff line change
@@ -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 <ARRAY>
_amount - Amount to select <NUMBER>
Returns:
New array with the specified amount of randomly selected elements <ARRAY>
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

0 comments on commit d38acc9

Please sign in to comment.