home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!spool.mu.edu!news.nd.edu!mentor.cc.purdue.edu!news
- From: ags@seaman.cc.purdue.edu (Dave Seaman)
- Subject: Re: Proving fair shuffles (was: FULL HOUSE)
- Message-ID: <BuMtr1.6vC@mentor.cc.purdue.edu>
- Sender: news@mentor.cc.purdue.edu (USENET News)
- Organization: Purdue University
- References: <psqghc8@fido.asd.sgi.com>
- Distribution: comp
- Date: Tue, 15 Sep 1992 18:23:24 GMT
- Lines: 47
-
- In article <psqghc8@fido.asd.sgi.com> mtj@babar.asd.sgi.com (Michael
- Jones) writes:
- [Re: honest shuffle]
- > Certainly such an algorithm exists, and is O(n):
-
- > while (old not empty)
- > begin
- > select a card in old at random
- > remove card from old
- > add card to new (anywhere: front, back, ...)
- > end
-
- This works if you consistently add the card at the front, or consistently
- at the back.
-
- > a more common sequence is the one
- > in my posted solution:
- >
- > old: array of 52 cards in any order
-
- > for (i = 0; i < 52; i++)
- > swap card[i] and card[random() % 52];
-
- This is incorrect. The algorithm generates 52^52 possible sequences of
- random numbers, each equally likely. Different transposition sequences may
- lead to the same final arrangement, but the problem is that 52! (the
- number of possible permutations) does not divide 52^52 (the number of
- transposition sequences that may be generated), thus guaranteeing that the
- permutations are not equally likely.
-
- A correct algorithm is
-
- for (i = 51; i > 0; i--)
- swap card[i] and card[random() % (i+1)]
-
- The fact that I have written the loop to execute for decreasing i is a
- minor point that makes the notation simpler. The major difference is the
- presence of "random() % (i+1)" instead of "random() % 52" in the swap.
-
- This algorithm generates exactly 52! possible sequences of transpositions,
- all equally likely. Each possible permutation corresponds to exactly one
- such sequence of transpositions. Therefore the result is an honest shuffle
- (if the random number generator is good).
-
- --
- Dave Seaman
- ags@seaman.cc.purdue.edu
-