PDA

View Full Version : Shuffle Algorithm

Japster
05-02-2005, 12:39 AM
I guess this question is more for Sklansky or Malmuth but here it is. What is the best way to create an unbiased shuffle in software. I know of some ways that would work but I was curious as to what you guys used and how accurrate it actually is. I am working on some software for learning different gambling games. Just curious.

Jack

Siegmund
05-02-2005, 02:36 AM
All you need is to select a uniform random number between 1 and 52! ~ 8.07x10^67 ~ 2^226. The result as as good as your random number generator is.

There are various sources for real random numbers such as HotBits (http://www.fourmilab.ch/hotbits/) or you can use the pseudo-RNG algorithm and very-large-integer package of your choice. Mersenne Twister seems to be the algorithm of choice these days.

If you don't require cryptographically strong randomness, e.g. for quick and dirty simulation code, it will probably make very little difference what you use. I freely admit that in my own programs, I use the good old-fashioned

FOR A = 1 TO 51
B = INT(RND*(53-A))+A
SWAP CARD(A),CARD(B)
NEXT

method with the system's built-in RNG. Nothing too bad happens. /images/graemlins/crazy.gif

LetYouDown
05-02-2005, 01:11 PM
The "swap card" method is the method I've used in some software I created where I wanted to just test the odds of getting certain hands. More of a hobby project than anything. Do you find that you can predict the order of the cards somehow, if this method is used? Say you were to swap 100k times.