Quant Memo
Foundational

Why the Naive Shuffle Is Biased

A classic interview trap — a shuffle algorithm that looks random but produces a non-uniform distribution over permutations, and how to spot the bias by counting how many ways each outcome can occur.

A common interview question asks you to write a shuffle function, and a common wrong-but-plausible answer is: for each position in the array, swap it with a position chosen uniformly at random from the entire array (not just the remaining unshuffled positions). It looks reasonable — every position gets a random swap — but it does not produce a uniform distribution over all n!n! permutations, and the bias is provable by simple counting.

The naive algorithm makes nn independent random choices, each with nn possible outcomes, so it can produce at most nnn^n distinct execution paths. But there are only n!n! possible permutations to land on. Since nnn^n is not generally divisible evenly by n!n! for n>2n>2, some permutations must be reachable by more execution paths than others, meaning some orderings come out more often than others — a bias that grows quickly with nn and shows up clearly even for a deck as small as 3 or 4 cards if you simulate it many times.

The fix is the Fisher-Yates shuffle: for each position from the last down to the second, swap it with a uniformly random position chosen only from the remaining unshuffled prefix (positions 1 through the current one), not the whole array. That algorithm makes exactly n,n1,,2n, n-1, \ldots, 2 independent choices, giving exactly n!n! equally likely execution paths — one for every permutation, with no bias possible by construction.

A shuffle that swaps each position with a uniformly random position from the whole array is biased, because it has nnn^n possible execution paths but only n!n! possible permutations to distribute them over; Fisher-Yates fixes this by restricting each swap to the remaining unshuffled prefix, giving exactly n!n! equally likely paths.

Related concepts

Practice in interviews

Further reading

  • Knuth, The Art of Computer Programming, Vol. 2
ShareTwitterLinkedIn