Is there such a beast as a "perfect" shuffle? :)

Christos Georgiou DLNXPEGFQVEB at spammotel.com
Fri May 10 11:55:19 EDT 2002


I needed a function that, given a sequence and a number, would produce
the <number>th permutation of the sequence.  I thought I once saw such a
function, but using google (web and groups) couldn't find it, so I wrote
one of my own (it's at the ASPN cookbook site:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/126037 )  
Basically, this was because I was aiming for a "perfect" shuffle of a
deck of 52 cards, so I wanted to reduce the "randomness" factor to only
one number.

My thoughts are that, if I can build a large enough number, 
a la:

(random.randint(0, sys.maxint)*(sys.maxint+1) + \
  random.randint(0, sys.maxint)
)*(sys.maxint+1) etc...

which optimistically would max beyond n! (perhaps (n+2)!), the number
would be a good candidate for a "truly" random shuffle, since the
function actually uses the (index mod n!) permutation.

Apart from that, which is just a naive approach, any hints / clues for
building good, uniform random numbers in the range 52! ?

Thanks in advance,
-- 
TZOTZIOY, I speak England very best,
Real email address: 'dHpvdEBzaWwtdGVjLmdy\n'.decode('base64')



More information about the Python-list mailing list