By bloid
via adrianquark.blogspot.com
Published: Sep 25 2008 / 16:06
Today at a career fair, an Amazon recruiter asked me how to shuffle an array. Sounds like an easy problem, right? It turns out a good majority of people posting code to the internet get it wrong. But before reading on, try it out yourself, see what you come up with, and then Google for "array shuffle" or "randomize array" to see what other people think.



Comments
amphi replied ago:
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
That algorithm basically puts everything into a bag and draws the items one by one. The cursor divides it into inside and outside the bag. If you think about it like that it should make more sense. Java's Collections.shuffle() for example utilizes this well-known algorithm.
A continuous variation of that algorithm (shuffles one item on-draw):
http://kaioa.com/node/53 (Never-ending Shuffled Sequences - When Random is too Random)
Voters For This Link (15)
Voters Against This Link (0)