Short answer: The Fisher-Yates shuffle, in the modern inside-out or the usual in-place form, produces each permutation of an array with equal probability. The in-place loop walks i from the end down to 1 and swaps i with a uniformly chosen index j in 0..i, inclusive. That is n−1 swaps and n−1 random integers. A common bug chooses j in 0..n−1 at every i, which does not give a uniform permutation.
What the problem actually asks
There are n! permutations. A shuffle is uniform when each of them has probability 1/n!. The in-place loop builds the permutation from the back: the item placed at i is equally likely to be any of the items still in the prefix, including the one already there. The number of possible execution traces is n·(n−1)·…·1 = n!, and they are equally likely, so the distribution is exact.
Choosing the swap partner from the whole array at every step produces n^n traces, which is not divisible by n! in a way that balances the permutations. Some orders come out more often than others. Sorting by random keys is uniform only when the keys do not tie, and it spends an unnecessary n log n.
What the algorithm guarantees
With a uniform independent integer in 0..i at step i, every permutation has probability 1/n!. Time is Θ(n) after the random integers are available. Extra memory is none for the in-place form. The algorithm does not need floating-point random numbers. A biased remainder, when a random 32-bit word is reduced modulo i+1 by a plain remainder, slightly prefers small residues. The practical repair is rejection sampling or a wide multiply. For a teaching implementation the mathematical statement assumes a truly uniform index.
The inside-out variant writes a new array and is the form to use when the source must stay intact. It has the same distribution.
The working steps
- For i from n−1 down to 1, draw j uniformly from 0, 1, …, i.
- Swap a[i] with a[j]. When j equals i the swap is a no-op and is a legal outcome.
- The index 0 is never the loop variable. It is already the last remaining item.
- Do not draw j from the whole array. The upper bound shrinks with i.
- To keep the source, for i from 0 to n−1 draw j in 0..i, set the new array’s i to the new array’s j, and set the new array’s j to source[i].
Uniformity needs the shrinking range. A random-key sort is a different algorithm and pays for a comparison sort. Cryptographic shuffles need a cryptographic random source and the same index rule. The algorithm permutes an array. It does not sample a subset. Reservoir sampling does that.
Approaches that look simpler and fail
| Approach | Why it fails when the swap index ignores the shrinking range |
|---|---|
| Draw j from 0..n−1 at every i | The permutation is not uniform. Some orders are reachable by more traces than others. |
| Draw j from 0..i−1, excluding i | The item at i is forced to move. Permutations that fix i at that step are missing, and the last iterations cannot leave a prefix in place. |
| Sort by random floating keys | Ties, however rare, break the intended uniform draw, and the cost is n log n. The swap loop is linear and exact. |
| Use a remainder of a random word without rejection | The low residues are slightly more likely when the modulus does not divide the random range. The bias is small and is not the uniform model the proof uses. |
A random subset of unknown length from a stream is reservoir sampling. A uniform permutation of an array in hand is Fisher-Yates. A random-key sort is unnecessary for this job.
A working example
This Python fragment swaps index i with a uniform index in 0…i, walking downward. The seeded permutation of 0 1 2 3 is 2 0 1 3.
import random
rng = random.Random(0)
a = [0, 1, 2, 3]
for i in range(len(a) - 1, 0, -1):
j = rng.randrange(i + 1)
a[i], a[j] = a[j], a[i]
assert sorted(a) == [0, 1, 2, 3]
print(*a)
Where it is used
Fisher-Yates is the in-place shuffle in Python random.shuffle and in every card or playlist shuffle that is actually uniform. Each index swaps with one later index. A sort by random keys is a different shuffle and can collide.
When you can drop this algorithm
Drop the full-range swap. Use the shrinking upper bound. Keep rejection sampling on the random source when the remainder bias matters.
Why this stays published
We publish this so a search for Fisher-Yates returns the shrinking index range that makes every permutation equally likely. The Fisher-Yates shuffle swaps each index i with a uniform index in 0..i, which produces every permutation with probability 1/n! in linear time.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
