ORLOV GAMES

Blog · Video Poker ·

How shuffling works in a digital card game

A 52-card deck has about 8 × 1067 orderings. One algorithm reaches all of them in 51 swaps, and the variant of it that looks identical makes some orderings 25% more likely than others.

A physical deck gets shuffled imperfectly and mostly gets away with it. A digital deck has no such margin. It is shuffled by a few lines of code which either produce every ordering with equal probability or do not, and the difference never shows up on screen — it shows up in a frequency table after a few million hands. There is one standard way to do it correctly, one well-known way to get it wrong that looks more thorough, and one real limit unrelated to either.

The size of the problem

A deck of 52 cards can be arranged in 52 factorial ways: 52 × 51 × 50 and so on down to 1, which comes to roughly 8.07 × 1067. Calling a deck shuffled is a claim about that number: that each of those orderings is exactly as likely as every other. It is a stronger claim than it sounds, and it is what the code must deliver.

Fisher–Yates, in 51 swaps

The correct algorithm is Fisher–Yates, and it is short enough to state in a sentence. Walk a counter i from the last card down to the second. At each step pick a position j uniformly at random from 0 up to and including i, swap the cards at i and j, and move on. For 52 cards that is 51 swaps and 51 random numbers, and the deck is shuffled in one pass.

The reason it works is a counting argument. The first step has 52 choices of j, the second 51, the third 50, down to 2 at the last step: 52! distinct runs of the algorithm, each equally likely because each choice was uniform. There are also exactly 52! orderings, and no two runs produce the same one. A one-to-one map between equally likely runs and possible orderings is precisely what a uniform shuffle means, so the shuffle is uniform — not approximately, exactly.

The off-by-one that breaks it

The tempting variant picks j from the whole deck every time — 0 to 51 — rather than from 0 to i. It looks like it should be better, because every card can move anywhere at every step. It is not uniform, and the counting argument shows why immediately: there are now 5252 equally likely runs rather than 52!, and 5252 is not a multiple of 52!, so the orderings cannot possibly come out even.

Three cards make it concrete enough to check by hand. The broken version has 33 = 27 equally likely runs spread across 6 orderings, which would need 4.5 runs each. Instead three orderings get 5 runs and three get 4, so one set is 25% more likely than the other. At four cards it is worse: 44 = 256 runs over 24 orderings, and the counts range from 8 to 15, meaning the most likely ordering arrives nearly twice as often as the least. The bias does not fade as the deck grows. It just stops being something you could notice.

Where the real ceiling is

A correct shuffle is still only as good as the random numbers feeding it, and this is where a browser or phone game has a genuine limitation. Math.random in the Chromium engine these games run inside is a generator called xorshift128+, which carries 128 bits of state. That caps the number of distinct sequences it can ever produce at 2128, about 3.4 × 1038. Set that against 8.07 × 1067 orderings and the reachable fraction is around 4 × 10-30. Almost every ordering of the deck cannot be produced at all.

That sounds fatal and is not, and the distinction is the useful part of this post. A video poker hand never exposes a 52-card ordering. It exposes five cards, and then up to five more. There are 52 × 51 × 50 × 49 × 48 = 311,875,200 ordered five-card deals, and 2,598,960 distinct five-card hands. Spread 3.4 × 1038 possible starting states over 311 million deals and each deal still has on the order of 1030 states behind it — uniform to far more decimal places than any amount of play could measure. The weakness of Math.random is that its future output is predictable to anyone who knows its internal state, which is exactly why it is the wrong tool when money is attached and a perfectly adequate one for dealing five cards to a practice game.

What the shuffle does not carry

Two details follow from uniformity, and both are where people look for a rigged deal.

The first is that nothing survives a hand. A new deck is built and shuffled from scratch before each deal, so the previous hand contributes nothing — which is why counting cards cannot work at a machine and why a long dry run does not make the next hand likelier to pay.

The second is that it does not matter which end of the deck the cards come from. An implementation might deal the five cards off the top and take draw cards off the bottom, which sounds like it ought to mean something. After a uniform shuffle every position in the deck has the same distribution, so top, bottom and middle are indistinguishable, and the choice is a convenience for whoever wrote the code rather than a fact about the game. The same goes for shuffling twice: a uniform shuffle applied to an already-uniform deck is still uniform, so the second pass buys nothing.

Shuffle quality and deal fairness are separate questions, and conflating them is the easy mistake. A game can run a textbook Fisher–Yates over a 52-card deck and then change one of the five cards before drawing it on screen, and nothing about the shuffle would reveal it. Whether the shuffle is uniform is answerable from a few lines of code. Whether the hand you are looking at is the shuffle's unmodified output is a different question, and on a free-chip game with no regulator the honest answer is often no — including the one below.

The summary is short. Fisher–Yates over 0 to i, 51 swaps, every ordering equally likely; the whole-deck variant is biased, invisibly so past about five cards. The generator cannot reach most orderings of a full deck, and for a five-card game that is irrelevant. None of it answers what happens between the shuffle and the glass, which is the only part of a digital deal worth being suspicious about.

Video Poker: Jacks or Better shuffles exactly as described above — a fresh 52-card deck before every hand, Fisher–Yates over 0 to i, 51 swaps, driven by Math.random. It is also a free-chip practice game rather than a model of a regulated machine, so the paragraph above applies to it: the five dealt cards are not always the shuffle's untouched output. A dead deal is sometimes nudged towards a pair or a four-card flush, more often after a run of losses or when your balance is nearly gone, and a deal that already contains a pair or better is left alone. The draw is untouched — replacement cards come straight off the remaining shuffled deck — so the hold decisions you practise are the real ones even though hands arrive more kindly than the maths would send them. Use it to drill hold priorities rather than to judge how often hands turn up. It has four machines, each showing its own unchanging chart: Jacks or Better from the start, Deuces Wild at level 3, Bonus Poker at level 8 and Double Bonus at level 15, with Triple and Five Play at level 10. You start with 1,000 chips, bet 5, 10, 20 or 50 a hand, and collect 200 more once a day; strategy hints will glow the suggested hold. It plays offline, and the chips have no cash value.

Get it on Google Play