From d8d783b7798f73351d386bc6f9cdfaa8b358cf6f Mon Sep 17 00:00:00 2001 From: NeilZar Date: Sat, 26 Jan 2019 03:45:01 +0100 Subject: [PATCH 01/10] Initial commit --- addons/arrays/CfgFunctions.hpp | 1 + addons/arrays/fnc_selectRandom.sqf | 37 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 addons/arrays/fnc_selectRandom.sqf diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index c7856c652..a0936d7d2 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(selectRandom); PATHTO_FNC(shuffle); PATHTO_FNC(sortNestedArray); }; diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf new file mode 100644 index 000000000..6d9509e50 --- /dev/null +++ b/addons/arrays/fnc_selectRandom.sqf @@ -0,0 +1,37 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_selectRandom + +Description: + Select a specified amount of elements from an array without picking the same element multiple times. + +Parameters: + _array - Input Array + _amount - Block + +Returns: + New array with the specified amount of randomly selected elements + +Example: + (begin example) + _result = [[1, 2, 3, 4, 5], 2] call CBA_fnc_selectRandom; + // _result => [2, 4] (random) + (end) + +Author: + NeilZar +---------------------------------------------------------------------------- */ +SCRIPT(selectRandom); + +params [["_array", [], [[]]], ["_amount", 0, [0]]]; + +if (_amount >= count _array) exitWith { _array }; +if (_amount <= 0) exitWith { [] }; + +private _result = []; + +for "" from 0 to _amount - 1 do { + _result pushBack (_array deleteAt (floor random (count _array))) +}; + +_result From 44c341aeeb328c3b92cedd00722f6d4402447143 Mon Sep 17 00:00:00 2001 From: NeilZar Date: Sat, 26 Jan 2019 03:57:54 +0100 Subject: [PATCH 02/10] Fixes and improvements --- addons/arrays/fnc_selectRandom.sqf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf index 6d9509e50..9cced949f 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectRandom.sqf @@ -30,8 +30,8 @@ if (_amount <= 0) exitWith { [] }; private _result = []; -for "" from 0 to _amount - 1 do { - _result pushBack (_array deleteAt (floor random (count _array))) +for "_i" from 0 to _amount - 1 do { + _result pushBack (_array deleteAt floor random count _array) }; _result From e92144efa10f92fc0636d0e35f6c8d42d56f456d Mon Sep 17 00:00:00 2001 From: commy2 Date: Sat, 26 Jan 2019 14:19:45 +0100 Subject: [PATCH 03/10] Update addons/arrays/fnc_selectRandom.sqf Co-Authored-By: neilzar --- addons/arrays/fnc_selectRandom.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf index 9cced949f..98d0b2979 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectRandom.sqf @@ -30,7 +30,7 @@ if (_amount <= 0) exitWith { [] }; private _result = []; -for "_i" from 0 to _amount - 1 do { +for "_i" from 1 to _amount do { _result pushBack (_array deleteAt floor random count _array) }; From 6c1f9b032e75b87d83ef1ddf07b57c89ecca01ad Mon Sep 17 00:00:00 2001 From: NeilZar Date: Sat, 26 Jan 2019 14:28:11 +0100 Subject: [PATCH 04/10] Implement reviews --- addons/arrays/fnc_selectRandom.sqf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf index 98d0b2979..802079ad0 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectRandom.sqf @@ -7,7 +7,7 @@ Description: Parameters: _array - Input Array - _amount - Block + _amount - Amount to select Returns: New array with the specified amount of randomly selected elements @@ -25,8 +25,8 @@ SCRIPT(selectRandom); params [["_array", [], [[]]], ["_amount", 0, [0]]]; -if (_amount >= count _array) exitWith { _array }; -if (_amount <= 0) exitWith { [] }; +_amount = _amount max (count _array); +_array = + _array; private _result = []; From 5968079bd411dbf5ad17acfce9b0478b1ee045f4 Mon Sep 17 00:00:00 2001 From: NeilZar Date: Sat, 26 Jan 2019 14:33:40 +0100 Subject: [PATCH 05/10] I want a min there --- addons/arrays/fnc_selectRandom.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf index 802079ad0..ad39b1c67 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectRandom.sqf @@ -25,7 +25,7 @@ SCRIPT(selectRandom); params [["_array", [], [[]]], ["_amount", 0, [0]]]; -_amount = _amount max (count _array); +_amount = _amount min (count _array); _array = + _array; private _result = []; From d0981bab283083deb5fd23e485aa526c055a879f Mon Sep 17 00:00:00 2001 From: commy2 Date: Sat, 26 Jan 2019 15:11:03 +0100 Subject: [PATCH 06/10] Add semicolon to please OCD Co-Authored-By: neilzar --- addons/arrays/fnc_selectRandom.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectRandom.sqf index ad39b1c67..977c31053 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectRandom.sqf @@ -31,7 +31,7 @@ _array = + _array; private _result = []; for "_i" from 1 to _amount do { - _result pushBack (_array deleteAt floor random count _array) + _result pushBack (_array deleteAt floor random count _array); }; _result From f1571da0c711d831ea84ba71e000c4252a625ed3 Mon Sep 17 00:00:00 2001 From: NeilZar Date: Sat, 26 Jan 2019 16:15:33 +0100 Subject: [PATCH 07/10] Rename to selectSomeRandom --- addons/arrays/CfgFunctions.hpp | 2 +- .../arrays/{fnc_selectRandom.sqf => fnc_selectSomeRandom.sqf} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename addons/arrays/{fnc_selectRandom.sqf => fnc_selectSomeRandom.sqf} (93%) diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index a0936d7d2..938aa4bfb 100644 --- a/addons/arrays/CfgFunctions.hpp +++ b/addons/arrays/CfgFunctions.hpp @@ -15,7 +15,7 @@ class CfgFunctions { PATHTO_FNC(join); PATHTO_FNC(reject); PATHTO_FNC(select); - PATHTO_FNC(selectRandom); + PATHTO_FNC(selectSomeRandom); PATHTO_FNC(shuffle); PATHTO_FNC(sortNestedArray); }; diff --git a/addons/arrays/fnc_selectRandom.sqf b/addons/arrays/fnc_selectSomeRandom.sqf similarity index 93% rename from addons/arrays/fnc_selectRandom.sqf rename to addons/arrays/fnc_selectSomeRandom.sqf index 977c31053..978c32f8f 100644 --- a/addons/arrays/fnc_selectRandom.sqf +++ b/addons/arrays/fnc_selectSomeRandom.sqf @@ -1,6 +1,6 @@ #include "script_component.hpp" /* ---------------------------------------------------------------------------- -Function: CBA_fnc_selectRandom +Function: CBA_fnc_selectSomeRandom Description: Select a specified amount of elements from an array without picking the same element multiple times. @@ -21,7 +21,7 @@ Example: Author: NeilZar ---------------------------------------------------------------------------- */ -SCRIPT(selectRandom); +SCRIPT(selectSomeRandom); params [["_array", [], [[]]], ["_amount", 0, [0]]]; From b86bc1df195caca9edaff2f04b9472f9d8f2a83b Mon Sep 17 00:00:00 2001 From: commy2 Date: Fri, 8 Feb 2019 15:38:59 +0100 Subject: [PATCH 08/10] function name --- addons/arrays/CfgFunctions.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/CfgFunctions.hpp b/addons/arrays/CfgFunctions.hpp index 938aa4bfb..b9ea0c675 100644 --- a/addons/arrays/CfgFunctions.hpp +++ b/addons/arrays/CfgFunctions.hpp @@ -15,7 +15,7 @@ class CfgFunctions { PATHTO_FNC(join); PATHTO_FNC(reject); PATHTO_FNC(select); - PATHTO_FNC(selectSomeRandom); + PATHTO_FNC(selectRandomArray); PATHTO_FNC(shuffle); PATHTO_FNC(sortNestedArray); }; From 5f9f7244b529e8ff827a28cd07008a74bcdf07d8 Mon Sep 17 00:00:00 2001 From: commy2 Date: Fri, 8 Feb 2019 15:40:47 +0100 Subject: [PATCH 09/10] update function name --- ...fnc_selectSomeRandom.sqf => fnc_selectRandomArray.sqf} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename addons/arrays/{fnc_selectSomeRandom.sqf => fnc_selectRandomArray.sqf} (88%) diff --git a/addons/arrays/fnc_selectSomeRandom.sqf b/addons/arrays/fnc_selectRandomArray.sqf similarity index 88% rename from addons/arrays/fnc_selectSomeRandom.sqf rename to addons/arrays/fnc_selectRandomArray.sqf index 978c32f8f..6a5fe9177 100644 --- a/addons/arrays/fnc_selectSomeRandom.sqf +++ b/addons/arrays/fnc_selectRandomArray.sqf @@ -1,12 +1,12 @@ #include "script_component.hpp" /* ---------------------------------------------------------------------------- -Function: CBA_fnc_selectSomeRandom +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 - Input Array _amount - Amount to select Returns: @@ -14,7 +14,7 @@ Returns: Example: (begin example) - _result = [[1, 2, 3, 4, 5], 2] call CBA_fnc_selectRandom; + _result = [[1, 2, 3, 4, 5], 2] call CBA_fnc_selectRandomArray; // _result => [2, 4] (random) (end) @@ -25,7 +25,7 @@ SCRIPT(selectSomeRandom); params [["_array", [], [[]]], ["_amount", 0, [0]]]; -_amount = _amount min (count _array); +_amount = _amount min count _array; _array = + _array; private _result = []; From bb4df442962583e45e119368fe466481ea569882 Mon Sep 17 00:00:00 2001 From: commy2 Date: Fri, 8 Feb 2019 15:41:03 +0100 Subject: [PATCH 10/10] update function name --- addons/arrays/fnc_selectRandomArray.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/arrays/fnc_selectRandomArray.sqf b/addons/arrays/fnc_selectRandomArray.sqf index 6a5fe9177..d9abe1666 100644 --- a/addons/arrays/fnc_selectRandomArray.sqf +++ b/addons/arrays/fnc_selectRandomArray.sqf @@ -21,7 +21,7 @@ Example: Author: NeilZar ---------------------------------------------------------------------------- */ -SCRIPT(selectSomeRandom); +SCRIPT(selectRandomArray); params [["_array", [], [[]]], ["_amount", 0, [0]]];