Unsorting(randomizing) a sequence

Christopher Browne cbbrowne at news.hex.net
Fri Aug 20 02:52:09 CEST 1999


Is it so much work to come up with a half-reasonable card shuffling
algorithm?

The one I've always seen cited as providing decent behaviour looks
thus:

for (i = 0; i > n; i++) {
   swap (item[i], item[random(n)]);
}

This algorithm guarantees that each and every item on the list gets
swapped with something.

If I wanted to make sure the permutation *really* looks random, I'd
consider running this "shuffle" multiple times, and would make sure I
had a RNG of decent quality with a period Rather Longer than n.
-- 
"For systems, the analogue of a face-lift is to add to the control
graph an edge that creates a cycle, not just an additional node."
-- Alan Perlis
cbbrowne at hex.net- <http://www.hex.net/~cbbrowne/languages.html>




More information about the Python-list mailing list