I would like to make a for loop that loops through the numbers 0-8 in a random order. Note that every number can only be visited once.
How can I achieve this?
I would like to make a for loop that loops through the numbers 0-8 in a random order. Note that every number can only be visited once.
How can I achieve this?
One possibility:
Enumerable.Range(0, 9).ToList()
creates a list containing the numbers from 0 to 8. Then in the loop we choose a random number from the list and remove it from the list at the end of the loop, so that the next cycle it can't be chosen again.Found this from a web search - Fisher-Yates shuffle, implemented in Perl.
This will generate an unbiased randomization of any input array.
For more info:
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
And the original I found was from:
http://perl.livejournal.com/101830.html