I have very big multi dimension array for example 2d inside for loop. I would like to return one element from this array at each iteration and this element should not returned before. I mean return an element once in the iteration.
-
1Please show some code for what you've tried and where you are stuck.Krease– Krease2016-01-19 17:24:24 +00:00Commented Jan 19, 2016 at 17:24
-
Will you eventually take all (or most) of the elements? I.e. is the number of iterations equal to (or close to) the number of elements in the array?Warren Weckesser– Warren Weckesser2016-01-19 18:15:47 +00:00Commented Jan 19, 2016 at 18:15
Add a comment
|
1 Answer
Without seeing any code, this is what I would try.
- Make an identically sized 2D array with just Booleans all set to True (available) by default
- When your code randomly generates an X,Y location in your 2D array, check the Availability array first:
- If the value at that location is True (available), return that value in the other Array (whatever values are stored there) and then set that available value to False.
- If the value at that location is False (not available), keep trying the next value in the array until you find one available. (Do this instead of hitting the random number generator again. The less elements available, the more you'd have to "re-roll" which would eventually become painfully slow.)
Make sense?
EDIT: I can think of at least 2 other ways of doing this that might be faster or more efficient, but this is the simple verson.