The Maths of Shuffling: 52!, Seven Riffles and Fair Card Games
Short answer: A deck of 52 cards can be arranged in 52! ways, about 8 × 1067, so a properly shuffled deck is almost certainly in an order that has never existed before. By hand, about seven riffle shuffles are needed to mix a deck well (Bayer and Diaconis, 1992); a few overhand shuffles are not nearly enough. Computers shuffle with the Fisher–Yates algorithm, which gives every order exactly the same chance, as long as it is written correctly.
- Solved games: tic-tac-toe, Connect Four, checkers
- Sudoku by the numbers
- The maths of shuffling (you are here)
- Solitaire odds: how many deals can be won?
Every card game starts with a moment nobody thinks about: the shuffle. It decides whether a game of Solitaire can be won, whether a poker hand is fair and whether two players get an equal chance. Behind it sits some of the most satisfying mathematics in games, from a number too large to imagine to a simple algorithm that is surprisingly easy to get wrong.
52 factorial: a number too big to picture
How many different orders can a deck of 52 cards be in? There are 52 choices for the top card, 51 for the next, 50 for the one after, and so on down to 1. Multiplying them gives 52 factorial, written 52!:
52! = 80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000
That is roughly 8 followed by 67 zeros. To get a feeling for it: if every person alive today had shuffled a deck once a second since the Big Bang, about 13.8 billion years ago, all of those shuffles together would come to around 1027 decks. That is not even a billionth of a billionth of a billionth of all possible orders. So when you shuffle a deck well, you are almost certainly holding an order that no one has ever held before and no one ever will again.
How many times should you shuffle?

Shuffling by hand only mixes the cards a little at a time, so the real question is how many shuffles it takes before the order is effectively random. In 1992 the mathematicians Dave Bayer and Persi Diaconis (a former professional magician) answered it for the riffle shuffle, where you split the deck and let the two halves fall into each other.
Their result was that the deck stays noticeably ordered for the first few riffles and then becomes random quite suddenly, at around seven shuffles. With fewer, traces of the original order survive: cards that were next to each other tend to stay in the same sequence, and a skilled player or magician can exploit that. More than seven helps only a little.
The familiar overhand shuffle, where you drop small packets from one hand to the other, is far weaker. It moves cards only short distances, so it takes thousands of overhand shuffles to reach the same level of mixing. A casual game where the deck gets two or three overhand shuffles is far from random.
A perfect riffle, where the halves interleave exactly one card at a time, is not random at all. Do eight perfect "out-shuffles" in a row (keeping the top card on top) and a 52-card deck returns to its original order. Magicians practise exactly this.
How a computer shuffles: Fisher–Yates
A computer does not riffle; it picks the order directly. The standard method is called the Fisher–Yates shuffle, after the statisticians Ronald Fisher and Frank Yates, who described a pencil-and-paper version in 1938. The computer version, published by Richard Durstenfeld in 1964 and made famous by Donald Knuth, works like this:
- Start with the last card in the deck.
- Pick a random position from the first card up to and including this one, and swap the two cards.
- Move one card towards the front and repeat until you reach the first card.
For 52 cards that is 51 swaps, and every one of the 52! orders comes out with exactly the same probability. Every card game on this site, including Solitaire, FreeCell, Spider Solitaire and Golf Solitaire, deals with this algorithm.

Shuffles that went wrong
Fisher–Yates is short, which makes it easy to change slightly and break. Three classic mistakes:
- Swapping with any position. A natural-looking version swaps each card with a random position anywhere in the deck, not just among the cards not yet shuffled. With 3 cards that creates 27 equally likely sequences of swaps, but 27 cannot be split evenly among the 6 possible orders, so some orders come up 5 times in 27 and others only 4. With 52 cards the bias is smaller but still real.
- Sorting with a random rule. Another shortcut is to "sort" the cards with a comparison that answers at random. The result depends on the sorting method and is often badly biased. In 2010 a browser choice screen that Microsoft showed to European Windows users was found to place the browsers unevenly for exactly this reason.
- Too few random starting points. In 1999 security researchers showed that an online poker site seeded its shuffle from the computer's clock. The seed counted milliseconds since midnight, so only about 86 million decks were possible out of 52!, and by syncing with the site's clock and watching their own cards they could work out everyone else's.
Can a computer really reach every order?
Here is a subtle point that surprises programmers. A random number generator in a computer has a limited internal state. Browsers today use generators with 128 bits of state, which can produce at most 2128, about 3 × 1038, different sequences. To reach every one of the 52! orders, a generator would need at least 226 bits. So, strictly, no browser game can produce every possible deck.
Does it matter for a game? No. The decks it can produce are spread evenly and unpredictably among all the possibilities, and 1038 is still vastly more than the number of games anyone will ever play. Casinos and online gambling sites, where money and cheating are involved, use certified hardware random sources instead; for Solitaire, a good shuffle algorithm and a modern browser generator are more than fair.
What this means at the table
- Shuffle seven times. If you play cards with family or friends, seven riffles (or a long, thorough wash of the cards on the table) are what it takes. Two or three is not a fair shuffle.
- Do not read meaning into a deal. A run of bad Solitaire deals is not the game being unfair; with orders spread evenly, streaks happen by chance.
- Every deal is new. There is no point memorising deals in an online card game that shuffles properly: you will never see the same one twice.
Did you know?
- Persi Diaconis left home at 14 to travel with a magician and only later became a professor of mathematics and statistics at Stanford University.
- Eight perfect out-shuffles restore a 52-card deck, but if you keep the top card second each time (an "in-shuffle"), it takes 52 of them.
- Microsoft's FreeCell numbered its deals by the seed of its random number generator, which is why players around the world can talk about "game 11982".
Next: Part 4: Solitaire odds, how many deals of Klondike, FreeCell, Spider and Golf can actually be won, including our own computer simulation.
