Sub topic 2
What Makes a Shuffle Fair
This chapter assumes you can read simple Python, know what a uniform distribution is, and remember from the previous chapter that a list of n distinct items has n! orderings, called permutations.
The definition
A shuffle is fair (or uniform) when every one of the n! permutations of the input is equally likely to come out. For a list of n distinct items, each permutation must appear with probability exactly 1/n!.
For [1, 2, 3] there are 3! = 6 permutations, so a fair shuffle returns each of them with probability 1/6. A shuffle that returns some orderings with probability 1/5 and others with 1/7 is biased, even if all six can still occur.
What the definition rules out
The definition makes three requirements:
- Every permutation is reachable. If any ordering has probability zero, the shuffle is not fair.
- No permutation is favored. Being reachable is not enough. The probabilities must all be equal.
- The input order does not leak through. Because every output has probability 1/n!, the result tells you nothing about the order the items started in.
A common mistake: checking one position at a time
A fair shuffle puts each item in each position with probability 1/n. A tempting test is to check only that property. It is necessary, but it does not prove fairness.
Here is a counterexample. This "shuffle" rotates the list by a random amount:
import random
from collections import Counter
def rotate_shuffle(items):
k = random.randrange(len(items))
return items[k:] + items[:k]
trials = 60000
perm_counts = Counter()
position_counts = Counter()
for _ in range(trials):
p = tuple(rotate_shuffle([1, 2, 3]))
perm_counts[p] += 1
for pos, x in enumerate(p):
position_counts[(pos, x)] += 1
print(len(perm_counts), "distinct permutations out of 6")
for key in sorted(position_counts):
print("position", key[0], "item", key[1], position_counts[key])Every (position, item) count comes out near 20000, which is exactly 1/3 of the trials. Judged one position at a time, the shuffle looks perfect. But it produces only 3 distinct permutations: (1, 2, 3), (2, 3, 1), and (3, 1, 2). The orderings (1, 3, 2), (2, 1, 3), and (3, 2, 1) never appear.
Why the exact count matters
Small biases matter because shuffles are usually run many times. A card game, a randomized experiment, or a load balancer repeats its shuffle thousands or millions of times. Over that many runs, a permutation that is 1% more likely shows up reliably, and anyone who knows about the skew can exploit it.
The definition also gives you a quick counting check that later chapters use. Suppose an algorithm makes random choices that lead to M equally likely outcomes. It can only be fair if those M outcomes split evenly across the n! permutations, so n! must divide M. If n! does not divide M, the algorithm is biased, and you can know this without running a single trial.
| n | Permutations (n!) | Fair probability of each |
|---|---|---|
| 3 | 6 | 1/6 ≈ 0.167 |
| 4 | 24 | 1/24 ≈ 0.042 |
| 10 | 3,628,800 | ≈ 2.8 × 10⁻⁷ |
| 52 | ≈ 8.07 × 10⁶⁷ | ≈ 1.24 × 10⁻⁶⁸ |
Exercise
A shuffle for 4 items can return only 12 different orderings, each with probability 1/12. Is it fair? Explain using the definition.
Show answer
No. Four items have 4! = 24 permutations. A fair shuffle must return each one with probability 1/24. This shuffle never returns 12 of them, so those 12 have probability 0, and it favors the other 12 at 1/12 each.
Exercise
An algorithm makes choices that produce 27 equally likely outcomes when shuffling 3 items. Without running it, can it be fair?
Show answer
No. There are 3! = 6 permutations, and 27 is not divisible by 6. The 27 outcomes cannot be shared equally among 6 permutations, so some permutations must be more likely than others.