Reading preferences
Concept

Fisher–Yates Shuffle

Rearrange a row randomly without confusing a shuffle with a sort.

shuffle, fisher yates, knuth shuffle, randomness, permutation

Mix a row of cards

Sorting puts cards in a chosen order, such as smallest to largest. Shuffling rearranges them instead. Imagine drawing one card fairly from the remaining cards to fill the last position, then repeating for the next position.

The Fisher–Yates shuffle, also called the Knuth shuffle, does that with swaps. It aims to make each arrangement equally likely, provided each random choice is uniform over its allowed indices given all earlier choices.

Why use it and where it helps

Shuffling can randomise the order of game cards or of examples in a classroom activity. Whether a particular random source is suitable depends on the application: Python's ordinary random generator is not designed for secrets or security-sensitive choices.

Follow three cards

Start with labelled cards [A, B, C]. This is one possible path, not a prediction of every run:

Position to fillChosen indexSwapRow
20C with A[C, B, A]
11B with itself[C, B, A]

At position 2 there are three choices, 0 through 2. At position 1 there are two choices, 0 through 1. Choosing the current position is allowed: a card may remain where it is. The remaining position is already filled.

See the reasoning

Follow the example one step at a time

Follow one possible shuffle of three labelled cards. This is a prepared teaching demonstration, not live code execution.

Start

[A, B, C]   unfilled positions: 0–2

The choices shown are one possible run, not the only result.

Python illustration

import random


def shuffled(items, rng):
    result = items[:]
    for i in range(len(result) - 1, 0, -1):
        j = rng.randrange(i + 1)
        result[i], result[j] = result[j], result[i]
    return result


cards = ["A", "B", "C", "D"]
print(shuffled(cards, random.Random(7)))
print(cards)
print(shuffled([], random.Random(7)))

Prepared output — not live execution:

['D', 'B', 'A', 'C']
['A', 'B', 'C', 'D']
[]

The function copies a Python list, leaving the original unchanged. The seed makes this example repeatable in the tested Python environment; exact sequences can depend on the random implementation. The site does not run code or make random choices. The function assumes rng.randrange(stop) returns an index in the half-open range from zero up to stop.

Why the choices matter

For n distinguishable cards, there are n choices for the last position, then n - 1 for the next, down to 2. Each sequence of choices gives exactly one arrangement, and each arrangement corresponds to one such sequence. If each choice is uniformly distributed conditional on the earlier choices, each arrangement has probability 1 / n! (one divided by n factorial).

Always swapping with any position from the whole row at every step is a different algorithm and generally biases the result. Swapping only with an earlier position also excludes allowed arrangements. With repeated values, labelled occurrences still have equal chances but several arrangements can look identical.

Time, space, and common mistakes

For n items, the algorithm makes n - 1 random choices and swaps for n > 0, so time is O(n), assuming constant-time random draws and swaps. This example uses O(n) space for its copy; an in-place version can use O(1) extra space. Empty and one-item lists need no swaps. A seed helps reproduce an example; it is not evidence that a source is unpredictable or secure.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. How does shuffling differ from sorting?
2. In the backward Fisher–Yates step for position i which index may be selected?
3. What makes all labelled arrangements equally likely in this shuffle?
4. What happens to repeated values during a shuffle?

References and further reading