Re: Can random_shuffle be used to shuffle a deck of cards?
- From: "Igor Tandetnik" <itandetnik@xxxxxxxx>
- Date: Wed, 26 Sep 2007 09:15:43 -0400
"Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx> wrote in message
news:%23FNq6kDAIHA.5360@xxxxxxxxxxxxxxxxxxxx
Daniel Lidstrom wrote:
I recently read that in order to shuffle a deck of 52 cards, you
would need a random number generator with a perion greater than 52!
(about 10^67). How can the implementation of random_shuffle
guarantee to pick one of the 52! permutations? Let's say I shuffle
100 items (10^157 permutations), can random_shuffle still do it
correctly? How does the algorithm overcome the limitations of rand()?
Actually, for `std::random_shuffle' it's enough that `rand'
(or user provided function) returns a number 0 <= N < 52 in
order to shuffle an array of 52 elements. The implementation
of `std::random_shuffle' uses Fisher-Yates shuffle:
"Fisher-Yates shuffle"
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
Quoting from this article:
Limited PRNG state space
An additional problem occurs when the Fisher-Yates shuffle is used with
a pseudorandom number generator: as the sequence of numbers output by
such a generator is entirely determined by its internal state at the
start of a sequence, a shuffle driven by such a generator cannot
possibly produce more distinct permutations than the generator has
distinct possible states. Even when the number of possible states
exceeds the number of permutations, the irregular nature of the mapping
from sequences of numbers to permutations means that some permutations
will occur more often than others. Thus, to minimize bias, the number of
states of the PRNG should exceed the number of permutations by at least
several orders of magnitude.
For example, the built-in pseudorandom number generator provided by many
programming languages and/or libraries may often have only 32 bits of
internal state, which means it can only produce 2^32 different sequences
of numbers. If such a generator is used to shuffle a deck of 52 playing
cards, it can only ever produce a vanishingly small fraction of the 52!
? 2^225.6 possible permutations. Thus, it's impossible for a generator
with less than 226 bits of internal state to produce all the possible
permutations of a 52-card deck, and for a (reasonably) unbiased shuffle,
the generator ought to have at least about 250 bits of state.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
.
- Follow-Ups:
- Re: Can random_shuffle be used to shuffle a deck of cards?
- From: Alex Blekhman
- Re: Can random_shuffle be used to shuffle a deck of cards?
- References:
- Re: Can random_shuffle be used to shuffle a deck of cards?
- From: Alex Blekhman
- Re: Can random_shuffle be used to shuffle a deck of cards?
- Prev by Date: Re: Can random_shuffle be used to shuffle a deck of cards?
- Next by Date: Re: Can random_shuffle be used to shuffle a deck of cards?
- Previous by thread: Re: Can random_shuffle be used to shuffle a deck of cards?
- Next by thread: Re: Can random_shuffle be used to shuffle a deck of cards?
- Index(es):
Relevant Pages
|