Sub topic 3
The Naive Shuffle and Where Its Bias Comes From
This chapter assumes you can read simple Python, understand zero-based indexing, and know that a fair shuffle gives each of the n! permutations probability 1/n!. Here we look at the most common wrong shuffle and use a counting argument to show why it can never be fair.
The tempting implementation
Many programmers first write a shuffle like this. For each position, swap its element with a randomly chosen position anywhere in the list.
import random
def naive_shuffle(items):
n = len(items)
for i in range(n):
j = random.randint(0, n - 1) # any index: this is the bug
items[i], items[j] = items[j], items[i]
return items
print(naive_shuffle([1, 2, 3]))The code looks reasonable. Every element gets moved, and every choice is uniform. The output still is not uniform.
Counting the equally likely outcomes
Each run makes n independent choices of j, and each choice has n possible values. That gives n × n × ... × n = n^n equally likely choice sequences. Each sequence produces exactly one final ordering.
For n = 3 there are 3^3 = 27 sequences but only 3! = 6 permutations. If each permutation were equally likely, each would need 27 / 6 = 4.5 sequences. A permutation cannot be produced by half a sequence, so some permutations must get more sequences than others.
Enumerating every case exactly
You don't need random sampling to see the bias. You can replay all 27 choice sequences in a fixed order and count the results:
from collections import Counter
from itertools import product
def naive_with_choices(items, choices):
items = list(items)
for i, j in enumerate(choices):
items[i], items[j] = items[j], items[i]
return tuple(items)
counts = Counter(
naive_with_choices([1, 2, 3], c) for c in product(range(3), repeat=3)
)
for perm, count in sorted(counts.items()):
print(perm, count, f"{count / 27:.3f}")| Permutation | Sequences | Probability |
|---|---|---|
| (1, 2, 3) | 4 | 0.148 |
| (1, 3, 2) | 5 | 0.185 |
| (2, 1, 3) | 5 | 0.185 |
| (2, 3, 1) | 5 | 0.185 |
| (3, 1, 2) | 4 | 0.148 |
| (3, 2, 1) | 4 | 0.148 |
A fair shuffle would give each permutation 1/6 ≈ 0.167. In this naive shuffle, some orderings are 25% more likely than others.
Why the bias holds for every n ≥ 3
For a uniform result, n^n would have to be divisible by n!. It never is when n ≥ 3. The number n − 1 divides n!. But n − 1 and n share no common factor, so n − 1 shares no factor with n^n either. When n − 1 > 1, it cannot divide n^n. For n = 3, the factor 2 divides 6 but does not divide 27.
Common mistakes in reasoning
- "Each swap is random, so the result is random." Every single step can be uniform while the combined result is not.
- "More swaps will fix it." Running the loop twice gives n^(2n) sequences, which still is not a multiple of n!.
- "The bias is tiny for large n." The distortion changes shape as n grows, but it does not disappear, and with large decks some orderings are much more likely than others.
The fix is to shrink the range of j at each step so that the number of choice sequences is exactly n!. That is the core idea of Fisher-Yates, covered in the next chapters.
Exercise
For n = 4, how many equally likely choice sequences does the naive shuffle have, and how many permutations? Use divisibility to explain why the result must be biased.
Show answer
There are 4^4 = 256 sequences and 4! = 24 permutations. 256 / 24 is not a whole number (256 = 2^8 has no factor of 3, but 24 does), so the sequences cannot be split equally among the permutations.
Exercise
A colleague changes the line to j = random.randint(0, n - 1) and loops only n - 1 times. Is the shuffle now fair for n = 3?
Show answer
No. There are 3^2 = 9 sequences and 6 permutations. 9 is not divisible by 6, so the shuffle is still biased.