Sub topic 10
When the Random Generator Is Too Small
This chapter assumes you know the in-place Fisher-Yates shuffle, that n items have n! orderings, and that a pseudorandom number generator (PRNG) produces a fixed sequence from a starting seed. It covers a limit that a correct shuffle loop cannot work around: the generator must be able to produce enough distinct outcomes.
The Pigeonhole Problem
A PRNG is deterministic. Its internal state fully decides every number it will produce next. When you run Fisher-Yates, the permutation you get is a function of the state the generator was in when the shuffle started.
If the generator can start from at most S distinct states, the shuffle can produce at most S distinct permutations. When S is smaller than n!, some permutations can never appear, however correct the loop is. Their probability is exactly zero, so the shuffle is no longer uniform.
How Many Bits You Need
To reach every permutation, the generator needs at least log2(n!) bits of state. This number grows fast:
import math
def bits_needed(n):
# log2(n!) computed through the log-gamma function, avoiding huge integers
return math.lgamma(n + 1) / math.log(2)
for n in (10, 52, 100, 1000):
print(n, round(bits_needed(n), 1))| n | Orderings | Bits of state needed |
|---|---|---|
| 10 | 3,628,800 | about 21.8 |
| 52 (a card deck) | about 8.07 × 10^67 | about 225.6 |
| 100 | about 9.33 × 10^157 | about 524.8 |
Now compare those figures with common generators:
| Generator | State or seed size | Full 52-card coverage? |
|---|---|---|
| 32-bit seed or 32-bit LCG | 32 bits | No |
| 64-bit generators (e.g. SplitMix64) | 64 bits | No |
| PCG64 | 128 bits | No |
| xoshiro256 | 256 bits | Possible in principle |
| Mersenne Twister (MT19937) | 19,937 bits | Yes, up to about 2,080 items |
A Real Failure
In the late 1990s, an online poker site shuffled with a correct algorithm but seeded its generator with the number of milliseconds since midnight. That allowed only about 86 million possible decks out of 52!. Researchers who synchronized with the server clock could narrow the possible decks to a small set and work out every player's hand after seeing a few cards.
Common Errors
- Seeding from the current time. The number of plausible seeds is tiny, and an attacker can guess them.
- Re-seeding before every shuffle. Each shuffle then has only as many outcomes as there are seeds, no matter how large the state is.
- Assuming a long period means full coverage. The period is how long the sequence runs before it repeats. Coverage depends on how many starting states your seeding can actually reach.
- Thinking reachability guarantees fairness. Having at least n! states is required, but you still need a good generator and unbiased index draws.
Decision Criteria
- Simulations, games, and tests: a well-seeded modern PRNG is fine. Missing permutations among 2^225 possibilities will not affect any statistic you can measure.
- Reproducible runs: a fixed seed is fine and useful, as long as you accept that the set of possible outcomes is limited.
- Gambling, lotteries, or anything adversarial: use a cryptographically secure source such as the operating system's generator, which keeps gathering fresh entropy and can't be predicted from a few outputs.
Exercise
A program shuffles a 52-card deck with a generator seeded from a 32-bit integer. Roughly what fraction of all possible decks can it ever produce?
Show answer
At most 2^32 decks out of 52!, which is about 2^225.6. The fraction is about 2^32 / 2^225.6 = 2^-193.6, effectively zero.
Exercise
What is the largest n for which a generator with exactly 64 bits of state could, in principle, reach every permutation of n items?
Show answer
n = 20. 20! is about 2.43 × 10^18, which is below 2^64 (about 1.84 × 10^19). 21! is about 5.11 × 10^19, which exceeds 2^64.