On this page
Shuffling a playlist or putting quiz questions in a random order needs a random permutation: every order exactly as likely as every other. The standard way to make one is the Fisher–Yates shuffle, and one small change to the random range makes it quietly wrong.
What is the Fisher–Yates shuffle?
Ronald Fisher and Frank Yates described the method in 1938 in their book Statistical Tables for Biological, Agricultural and Medical Research, as a pencil-and-paper method using a table of random numbers (Fisher–Yates shuffle, Wikipedia). The computer version was published by Richard Durstenfeld in 1964 as "Algorithm 235: Random permutation" in Communications of the ACM (DOI record). Donald Knuth popularised it in The Art of Computer Programming as "Algorithm P (Shuffling)", and it is also known as the Knuth shuffle (Wikipedia).
For an array a with positions 0 to n − 1, the modern version is:
For i from n − 1 down to 1:
pick a random integer j with 0 ≤ j ≤ i, every value equally likely;
swap a[i] and a[j].
Like drawing names from a hat, the last position gets a random item from all n, the next from the n − 1 left, and so on. Since j may equal i, an item may stay put.
Why does the Fisher–Yates shuffle give all n! orders equally?
Count the choices. The first step has n possible values of j, the next has n − 1, and so on down to 2, so there are n × (n − 1) × … × 2 = n! equally likely runs of the algorithm.
Each run produces a different order, because the choice of j at step i decides which remaining item lands in position i. So n! runs map one-to-one onto n! orders, and every order has probability exactly 1/n!. For 1, 2, 3 there are 3 × 2 = 6 runs, one per order.
Why is the naive shuffle biased, and by how much?
The tempting version swaps each position with a random position anywhere in the array: for i from 0 to n − 1, pick j from 0 to n − 1. In The Danger of Naïveté (2007), Jeff Atwood ran this naive shuffle 600,000 times on three cards and found that 231, 213 and 132 came up too often while the other three orders came up too rarely.
The cause is counting, not luck. The naive loop makes 3 choices of 3 values each, so there are 3³ = 27 equally likely runs, and 27 cannot be split evenly among 6 orders (Wikipedia). The exact tally from 123, checkable by hand:
After the swap at i = 0 (j = 0, 1, 2): 123, 213, 321.
After the swap at i = 1, nine states: 213, 123, 132 from the first; 123, 213, 231 from the second; 231, 321, 312 from the third.
The swap at i = 2 turns any state xyz into zyx, xzy or xyz, giving 27 final orders.
The result:
132, 213 and 231 each appear in 5 runs, a probability of 5/27, about 18.5%.
123, 312 and 321 each appear in 4 runs, a probability of 4/27, about 14.8%.
A fair shuffle gives each order 1/6, about 16.7%.
These are the three orders Atwood saw too often. The mismatch does not go away for larger n: for six cards the naive loop has 6⁶ = 46,656 runs over 6! = 720 orders (Atwood), and for any n > 2, n^n is never divisible by n!, so the naive shuffle is always biased (Wikipedia).
The opposite slip, choosing j strictly less than i, is Sattolo's algorithm, which produces only orders that form a single cycle, so no item ever stays in its starting place (Wikipedia). For 1, 2, 3 it can only give 231 and 312.
How fast is the Fisher–Yates shuffle?
The loop runs n − 1 times, one random number and one swap each, so it takes O(n) time and no extra memory. Durstenfeld's version cut the time to O(n) from O(n²) for a naive computer implementation of the original method (Wikipedia); our explainer on Big O notation and growth rates shows how far apart those get. Sorting on random keys is fair if no keys tie, but it needs memory for the keys and, with a comparison sort, more time, since comparison sorting needs about n log n comparisons (Wikipedia).
How do you check a shuffle you wrote?
Count: multiply the range sizes over all steps; the product must equal n!.
Fixed points: if no item ever stays in place, you have written Sattolo's algorithm.
Test small: shuffle 1, 2, 3 many times and check that each of the 6 orders turns up about equally often.
The argument is the multiplication principle from permutations and combinations, so it doubles as JEE practice. If n! and counting still feel shaky, the Fermi Concept Library has a lesson for every concept in mathematics and computing. For a counting problem that will not come out, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



