Jump to content
Spriterfight

HELP How to slelect random elements from an array with a specific number

Recommended Posts

So i have an array _array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"];

how can i select ex 4 random elements and loop through it because i dont want to loop thorug every element

Share this post


Link to post
Share on other sites

is it ok if same element is selected twice or more?

 

Share this post


Link to post
Share on other sites
3 minutes ago, gc8 said:

is it ok if same element is selected twice or more?

 

No because those elements are locations and i dont want the units to spawn every nearest location from the player so i want to select random locarions from this array with a limited number,If its duplicated then a group will be spawned twice at the same location

Share this post


Link to post
Share on other sites

will this script do?

 

_array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"];

for "_i" from 0 to 3 do
{
 if(count _array == 0) exitWith {};

 private _rindex = floor(random (count _array)); // Get random index

 private _randSel = _array # _rindex;

 systemchat format["Random entry selected: %1", _randSel ];

 _array deleteAt _rindex; // Delete from array to prevent selecting twice

};

 

the code could have check for empty _array if needed added

  • Like 3

Share this post


Link to post
Share on other sites
18 minutes ago, gc8 said:

will this script do?

 


_array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"];

for "_i" from 0 to 3 do
{
 private _rindex = floor(random (count _array)); // Get random index

 private _randSel = _array # _rindex;

 systemchat format["Random entry selected: %1", _randSel ];

 _array deleteAt _rindex; // Delete from array to prevent selecting twice

};

 

the code could have check for empty _array if needed

it seems to be working.Thank you!

Share this post


Link to post
Share on other sites

I believe the solution gc8 gave you is pretty much what you seek. One more thing is to be added in case you would like to keep the original array intact. In order to achieve this, you could possibly follow two different approaches (most probably there are more but I am not smart enough to think of them :D).

1) Copy the original array and use that in order to place your units. This would look like (refer to this site for more information about copying array with '+')

_array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"];

// Just copy the original array to another one
private _tempArray = +_array;

for "_i" from 0 to 3 do
{
 private _rindex = floor(random (count _tempArray)); // Get random index -> CHANGE _ARRAY TO _TEMPARRAY

 private _randSel = _tempArray # _rindex; // -> CHANGE _ARRAY TO _TEMPARRAY

 systemchat format["Random entry selected: %1", _randSel ];

 _tempArray deleteAt _rindex; // Delete from array to prevent selecting twice -> CHANGE _ARRAY TO _TEMPARRAY

};

2) You could possibly create an array with the elements that you get from the original array and compare the value you extract from the original array against those. This could be done like

_array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"];

// Declare empty array
private _chosenArray = [];

for "_i" from 0 to 3 do
{
 private _rindex = floor(random (count _array)); // Get random index
 private _randSel = _array # _rindex; // Get the value at the index

 while {_randSel in _chosenArray} do {
	_randSel = _array (floor(random (count _array))); // Get another random value from the array
 }

 systemchat format["Random entry selected: %1", _randSel ];

 _chosenArray pushBack _randSel; // Add to the _chosenArray
};

Not sure any of the two methods are useful to you or not, but they provide two ways to keep your original array intact. The second method also provides a way to see which positions have been used if the need arises.

 

Hope this helps somehow.

  • Like 2

Share this post


Link to post
Share on other sites

@ZaellixA Good point about copying the array. I forgot that and didn't know if it mattered to him

  • Like 2

Share this post


Link to post
Share on other sites

Don't iterate over an array while simultaneously deleting its elements:

_array = ["myArray",  "myArray1", "myArray2", "myArray3", "myArray4", "myArray5","myArray6",  "myArray7", "myArray8"]; 
_result=[]; 
while {count _result < 4} do { 
	_result pushBack (_array deleteAt (random floor count _array)) 
}; 
hint str _result

DeleteAt already returns the deleted element, so might as well use it.

 

Cheers

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×