Randomness and Generators

Shuffling a Deck Fairly: The Algorithm Behind It

Walk through the Fisher-Yates shuffle and see why naive swapping methods produce biased card orders, with checked counting examples and no betting claims.

On this page
  1. What “fair” means for a shuffle
  2. The correct method: Fisher-Yates
  3. The common mistake: swap with any position
  4. Limits set by the generator
  5. Checking a shuffle
  6. Cards, slots and what shuffling tells us

Card and slot-style games are games of chance with a built-in house edge. No strategy, system or timing changes the odds, most players lose money over time, and nobody should gamble money they cannot afford to lose. This article is about a puzzle in programming: how to put a deck of cards into a random order so that every possible order is equally likely.

What “fair” means for a shuffle

A deck of 52 cards can be arranged in 52 factorial ways, written 52!. That number is about 8.07 × 10^67, a figure with 68 digits. A fair shuffle gives each of those arrangements exactly the same probability. It is not enough that the top card looks random or that the shuffle seems thorough; the whole ordering must be unbiased.

For small decks the counting is easy to see. Three cards can be ordered in 3 × 2 × 1 = 6 ways. A fair shuffle should produce each of the six orders with probability 1/6.

The correct method: Fisher-Yates

The standard algorithm, named for its early describers and popularized for computers by Donald Knuth, works from the end of the list back to the start.

  1. Start at the last position.
  2. Pick a random position from the start of the list up to and including the current position.
  3. Swap the card at the current position with the card at the chosen position.
  4. Move one position toward the front and repeat, until only one position remains.

Why it works: for the last position, there are n equally likely choices, so each card has a 1/n chance of ending up there. For the next position, there are n − 1 choices among the remaining cards, and so on. The total number of equally likely sequences of choices is n × (n − 1) × … × 1 = n!, and each sequence leads to a different ordering. The count of choice sequences equals the count of orderings, with a one-to-one match, so each ordering is equally likely.

The common mistake: swap with any position

A popular but flawed version goes through each position in turn and swaps its card with a card at a random position anywhere in the deck, including positions already handled. It looks reasonable, and it is wrong.

The counting shows why. With three cards, the loop makes three choices, each from three positions, giving 3 × 3 × 3 = 27 equally likely sequences of choices. But there are only 6 possible orders, and 27 does not divide evenly by 6 (6 × 4 = 24, with 3 left over). So some orders must receive more of the 27 sequences than others; they cannot all get 4.5. The result is a bias, not just in theory but in actual frequency tables, and it grows more complicated with larger decks. For 52 cards, the method uses 52^52 choice sequences, which cannot be split evenly across 52! orders because 52! has prime factors, such as 3 and 47, that 52^52 lacks.

Other flawed approaches include:

  • Sorting by random keys using an inconsistent comparison. Telling a sorting routine that two items compare randomly can produce uneven results and, in some implementations, errors. Assigning each card one random key and then sorting by the keys is sound, provided keys rarely tie and are drawn from a good generator.
  • A few “riffle-style” passes. Imitating a hand shuffle too few times leaves structure. Physical shuffling research suggests that several riffle shuffles are needed before a deck is close to random, and fewer give detectable order.
  • Biased index picking. Using a remainder operation on a raw value can make some positions more likely, as described under outcome mapping.

Limits set by the generator

A shuffle is only as good as the numbers feeding it. A generator with a 32-bit seed can start in only about 4.3 billion different states, so it can produce at most about 4.3 billion different shuffles, a tiny share of the roughly 8 × 10^67 possible orders. That does not necessarily make results look unfair, but it means most orders can never occur, and an attacker who can guess the seed may be able to predict the deck. Systems that deal real-money card games therefore need a generator with a large state and a strong seed value drawn from good entropy sources. The cycle length also matters; see generator period.

Checking a shuffle

One practical test is to shuffle a very small deck many times and count how often each order appears. For three cards, run, say, 600,000 shuffles; each of the six orders should appear about 100,000 times, with ordinary random variation of a few hundred. A biased method shows persistent differences. Home experiments of this kind are a good use of chance simulations, and they build intuition about what fair really means. The same approach underlies formal randomness tests applied to larger systems.

Cards, slots and what shuffling tells us

Real card games differ in how they keep randomness: a physical shuffle is imperfect, a digital one can be exactly uniform if coded correctly, and some games use continuous shuffling machines. Slot-style games typically do not shuffle at all; they draw independent outcomes, often through reel strip weighting. In both cases, past cards or spins provide no reliable signal beyond what the known composition of the remaining cards implies, and no card-ordering knowledge turns the house edge into a player advantage in a regulated digital game.

If you play cards or any game of chance, set money and time limits ahead of time, treat it as paid entertainment, never chase losses, and take breaks. If gambling feels hard to control, a national problem-gambling helpline or local health service can offer support. Gambling is age-restricted or illegal in many places, and rules vary by country.

The complete guide to randomness and generators

How Computers Generate Random Numbers
Read the guide

Keep exploring

Guides from other topics

All topics →