How the Fisher–Yates shuffle works

How a simple loop of random swaps gives every possible ordering of a list exactly the same chance, and the small mistakes that quietly break that fairness.

Written by Amili, an AI writer, from the sources listed below · 24 October 2026 · 5 min read


The Fisher–Yates shuffle is a method for putting a finite list into a random order so that every possible arrangement is equally likely. Its modern form runs in time proportional to the list's length and works in place, which makes it the standard way to shuffle fairly.

In short

  • It picks a random element from the items not yet placed, fixes it in position, and repeats until none remain.
  • Ronald Fisher and Frank Yates described a pencil-and-paper version in 1938; Richard Durstenfeld adapted it for computers in 1964.
  • The modern version swaps items within the array, so it needs no extra list and runs in linear time.
  • Choosing the random index from the wrong range is a common bug that silently biases the result.
  • Shuffling by sorting with a random comparison function is unreliable and can even crash a program.

What is the Fisher–Yates shuffle?


A shuffle is fair when every ordering of the items is as likely as any other. For a deck of 52 cards that ideal is a random permutation, and such permutations are used in games of chance, simulation, cryptography and other randomised algorithms. The Fisher–Yates shuffle produces exactly this: an unbiased permutation, provided its source of random numbers is itself unbiased.

The method carries the names of its first describers, Ronald Fisher and Frank Yates, and is also called the Knuth shuffle after Donald Knuth. Their 1938 version appeared in a book of statistical tables and was meant to be done by hand, with a printed table of random numbers supplying the chance.

How does it work, step by step?


The original method is easy to picture. Write the numbers 1 to N in a row. Choose a random number k between one and however many numbers are still unstruck. Count along to the kth number that has not been crossed out, cross it out, and copy it onto the end of a new list. Repeat until everything is crossed out. The new list is the shuffled order.

A computer doing this literally wastes effort counting past crossed-out entries. Durstenfeld's improvement, later popularised by Knuth in The Art of Computer Programming, avoids that. Instead of crossing an item out, it swaps the chosen item with the last item not yet chosen, so the finished part of the array grows from the end. In practice the loop walks an index i from the last position down to the second, picks a random j from 0 to i inclusive, and swaps the items at j and i. That brings the running time down to linear and needs no second list.

What does a worked example look like?


Take four cards labelled A, B, C, D. First pick a random position from 1 to 4; say it is 2, so B swaps with the last card, D, giving A D C B. B is now fixed at the end. Next pick from 1 to 3; say 1, so A swaps with C, giving C D A B. Then pick from 1 to 2; say 2, which swaps D with itself, leaving C D A B. The single remaining card is already in place.

At each step every unplaced card had an equal chance of being chosen, so each of the 24 possible orders of four cards is equally likely to come out. Notice that a card is allowed to swap with itself; that detail matters, as the next section shows.

Where does it go wrong?


The most common bug is picking the random index from the wrong range. If j is always kept strictly below i, the code turns into Sattolo's algorithm, published in 1986 by Sandra Sattolo, which only ever produces orders forming one single cycle. With that change no item can stay in its original spot, and many orderings can never appear at all.

The opposite slip, choosing j from the whole array on every step, is biased too, though less obviously. With three items it gives 27 equally likely swap sequences but only 6 possible orders, and 27 does not split evenly into 6, so some orders turn up more often than others. Bias can also come from the random number generator: reducing a random value with a simple remainder skews results unless the generator's range is a multiple of the target, or vastly larger than it.

Why not just sort with random keys?


One alternative attaches a random number to every item and sorts by those numbers. It can be unbiased and has a simple parallel version, but it needs extra memory for the keys, and ties between keys must be avoided because sorting routines do not break ties randomly.

A far worse shortcut is sorting with a comparison function that answers at random. The outcome depends heavily on the sorting algorithm and is very likely to be uneven; with quicksort, for instance, the first pivot tends to end up near the middle. Worse, a sort may assume its comparisons are consistent, so random answers can cause endless loops or memory errors. Fisher–Yates, by contrast, has optimal time and space costs and can even be stopped partway when only part of the shuffle is needed.

What does it teach about thinking?


Fairness is easy to fake and hard to check by eye. A broken shuffle still looks random in any single run; the flaw shows only in the long-run counts of which orders appear. The Fisher–Yates loop works because each step gives every remaining option an equal chance, and the reasoning behind that is simple enough to verify. When a process looks right but cannot be argued right, it is worth counting the possible outcomes before trusting it.

Questions people ask


Is the Fisher–Yates shuffle truly unbiased?

Yes, as long as the random numbers it draws are unbiased and the index range is correct. Each possible ordering of n items then comes out with probability exactly one in n factorial. In real programs the quality also depends on the random number generator, so test suites check whether statistics of many shuffles match what true randomness predicts.

What is the difference between Fisher–Yates and Sattolo's algorithm?

They differ by one detail. Fisher–Yates lets the random index include the current position, so an item may swap with itself. Sattolo's algorithm excludes it. That small change means Sattolo's version only produces orderings that form a single cycle through all items, which is useful on purpose but a bug when an ordinary shuffle was intended.

Why is the naive swap-with-anything shuffle biased?

Swapping each item with one chosen from the whole list gives n to the power n equally likely sequences of swaps, but only n factorial possible orders. For more than two items the first number never divides evenly by the second, so some orders must be produced by more sequences than others and therefore appear more often.

The thinking behind it


It includes a chapter on randomness and why deliberately random procedures can be the most reliable choice.

Read or listen to Algorithms to Live By

Hear the whole book free: start an Audible trial and your first audiobook — this one, if you like — is on the house.

As an Amazon Associate, ReadGlobe earns from qualifying purchases and Audible trials — at no extra cost to you.

Sources

How this was made: Amili, an AI writer, wrote this article in its own words from the sources above. Every link was checked before publishing. Spotted an error? Tell us and we will correct it.

More algorithms, explained


Books readers reach for

As an Amazon Associate, ReadGlobe earns from qualifying purchases — at no extra cost to you.