Inside the Shuffle: Fisher–Yates and the Fair Deal
A memory game lives or dies on one promise: the layout you're dealt is random. Not "looks random" — actually random, meaning every possible arrangement of the cards is exactly as likely as every other. Memory Match keeps that promise with an algorithm older than the computer it runs on: the Fisher–Yates shuffle, published as a pencil-and-paper procedure by statisticians Ronald Fisher and Frank Yates in 1938, and adapted for computers by Richard Durstenfeld in 1964.
Why Not Just… Shuffle Randomly?
The intuitive approaches are all quietly broken:
- "Sort by a random key" — the infamous
arr.sort(() => Math.random() - 0.5)one-liner — hands a non-deterministic comparator to a sorting algorithm that assumes consistency. The result depends on the sort's internals and is measurably biased: some layouts come up far more often than others. When a browser vendor once used this trick to randomize a ballot order, the "random" list favored certain positions heavily. - "Swap random pairs a bunch of times" feels thorough but has no stopping rule that guarantees uniformity — after any fixed number of random swaps, some permutations remain more likely than others.
Bias in a memory game isn't cosmetic. If certain cards tend to land near each other, a player who grinds enough games could learn the tendency — and the game silently stops being about memory.
The Algorithm: One Pass, Perfectly Uniform
Fisher–Yates fixes it with a single backwards walk through the deck. At each position, pick a random card from the ones not yet placed, and swap it in:
function shuffle(deck) {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); // 0 ≤ j ≤ i
[deck[i], deck[j]] = [deck[j], deck[i]];
}
return deck;
}
The proof of fairness is a counting argument you can do on a napkin. The last slot is filled by a card chosen uniformly from all n; the next by one of the remaining n−1; and so on. Multiply the choices — n × (n−1) × … × 1 — and you get exactly n! equally likely outcomes: one for every possible arrangement of the deck. No permutation is privileged; no layout is impossible.
A 4×4 Memory Match board has 16 cards — that's 16! ≈ 20.9 trillion orderings. The 6×6 board's 36 cards have more arrangements than there are atoms in a human body. You will never see the same board twice.
Where Memory Match Uses It (Twice)
Every new game runs the shuffle two times:
- Choosing the symbols. Each theme ships at least 18 symbols, but a 4×4 board only needs 8. Rather than always dealing the first eight animals, the game shuffles the theme's full set and takes a random sample — so replays feel fresh even on the same theme.
- Laying out the board. The chosen symbols are duplicated into pairs, and the resulting deck is Fisher–Yates-shuffled into its final grid positions.
Both passes run in your browser in a fraction of a millisecond — the algorithm touches each card exactly once, which is also why it's still the standard shuffle in card games, lotteries and statistics libraries nearly ninety years after Fisher and Yates published it for use with a table of random numbers and a pencil.
What Fairness Means for Your Score
Because every layout is equally likely, there is no pattern to learn and no "hot corner" to exploit — the only edge available is the one you build between your ears. That's by design: it keeps the leaderboard-against-yourself honest. Your best score falls when your technique improves, not when the deck drifts your way. If you want the technique, start with the move-count math and the full strategy guide.