Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CBA_fnc_selectRandomArray #1049

Merged
merged 10 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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