Sub topic 4
The Original Fisher-Yates Method
This chapter assumes you can read simple Python loops and know that a fair shuffle gives each of the n! orderings the same probability. Before computers were common, statisticians needed random orderings for experiments. In 1938, Ronald Fisher and Frank Yates published a method that needs only a pencil, paper, and a table of random numbers. The in-place version that programmers use today comes from this method, so it helps to see the original first.
The Procedure
The 1938 description works on a written list of items:
- Write down the numbers (or items) 1 through N.
- Pick a random number k between 1 and the count of items not yet struck out, inclusive.
- Counting from the start of the list and skipping struck items, find the k-th unstruck item. Strike it out and write it at the end of a separate result list.
- Repeat steps 2 and 3 until every item is struck out.
The result list is the shuffled order. Fisher and Yates read k from printed tables of random digits. Today a library call supplies k.
A Worked Trace
Shuffle the items A, B, C, D, E:
| Step | Unstruck items | Range for k | k drawn | Struck | Result so far |
|---|---|---|---|---|---|
| 1 | A B C D E | 1–5 | 3 | C | C |
| 2 | A B D E | 1–4 | 4 | E | C E |
| 3 | A B D | 1–3 | 1 | A | C E A |
| 4 | B D | 1–2 | 2 | D | C E A D |
| 5 | B | 1–1 | 1 | B | C E A D B |
The range shrinks by one at each step, which matters for fairness. The first draw has 5 possible outcomes, the next has 4, and so on down to 1. That gives 5 × 4 × 3 × 2 × 1 = 120 = 5! possible draw sequences, and each sequence produces a different ordering. Each ordering therefore matches exactly one equally likely sequence of draws. Compare the naive shuffle from the previous chapter, which has n^n sequences that cannot be split evenly over n! orderings. A later chapter turns this counting idea into a full proof.
The Method in Python
This version keeps the "strike out" step instead of using list.pop, so you can see where the work goes:
import random
def original_fisher_yates(items):
struck = [False] * len(items)
result = []
remaining = len(items)
while remaining > 0:
k = random.randint(1, remaining) # 1-based, inclusive, as in 1938
count = 0
for idx, done in enumerate(struck):
if not done:
count += 1
if count == k:
struck[idx] = True
result.append(items[idx])
break
remaining -= 1
return result
print(original_fisher_yates(list("ABCDE")))Where It Falls Short
The method is fair, but it is slow and uses extra memory:
- Scanning cost. Finding the k-th unstruck item means walking past struck entries. Over all n steps this adds up to O(n²) time.
- Extra storage. It needs a struck-out marker for every item and a second list for the result, so it uses O(n) extra space.
- Common mistakes. People working by hand can lose track of which items they have already struck. A program avoids that, but the quadratic scan remains.
In 1964, Richard Durstenfeld removed the scan. His fix moves each chosen item to the end of the array by swapping it, so the unstruck items always stay together at the front of the array. The next chapter builds that version.
Exercise
You shuffle 4 items with the original method. How many values can k take at each step, how many distinct draw sequences are there in total, and roughly how many unstruck-item checks does the scan make in the worst case across all steps?
Show answer
k can take 4, 3, 2, then 1 values, so there are 4 × 3 × 2 × 1 = 24 = 4! draw sequences, one for each ordering. In the worst case each step scans the whole remaining list, about 4 + 3 + 2 + 1 = 10 checks. In general that is n(n+1)/2, which is O(n²).