card dealer

Ned Batchelder ned at nedbatchelder.com
Fri Sep 27 17:40:46 EDT 2013


On 9/27/13 12:10 PM, Denis McMahon wrote:
> On Fri, 27 Sep 2013 12:08:33 +0000, Dave Angel wrote:
>
>> i recall
>> writing a shuffle function in C decades ago, which took an array of (52)
>> unique items and put them in random order.
> Whenever I tried to write shuffles I came up against a fairly fundamental
> limit:
>
> 52! > prng states
>
> Granted prngs seem to be better on the importing entropy from elsewhere
> front these days.
>

Python's PRNG holds plenty of state to handle card shuffling. Python's 
random number generator has a period of 2**19337-1, so it has that many 
states.  That is much larger than 52!, about 10**5933 times larger.  
(Unless I've botched the math...)

The other variable is how many states the initial seed can have.  If 
os.urandom is available, then it seeds with 16 bytes of OS-supplied 
randomness.  But 52! is about 2**226, so the 128 bits of seed isn't 
enough to make all shuffles possible.

You could seed a Random yourself with more bits if you really wanted to.

--Ned.



More information about the Python-list mailing list