i have created a 2D array of GameObjects now i want to select randomly half of array elements

i know how to select one element randomly but i didn’t find any solution of selecting half of array elements randomly every time i play the game .

1 Answer

1

create an identical array of type bool. create another int to count up to half the total size of the array. randomize column and row and check the index of the bool array if it’s true (already picked). if not, set to true and use the indices in the other array, plus add one to the count int.if yes, continue randomization. repeat until count has reached half the size.

OR

create a list of Vector2 and add as many as you have rows and columns pairs. randomize it, use and remove the picked entry. repeat until list is as short as half its initial size.

P.S. you could create a struct of two int instead of using Vector2 which contains floats.