How the deals are made
The cipher, the seed, and what they can and cannot promise.
Card players check the shuffle, and engineers check the arithmetic. This page is for both. The proofs behind it come with the game’s source: docs/math/ proves each claim, and docs/teaching/ walks through the code with exercises.
In short
- Every deal is shuffled with ChaCha20, the stream cipher the IETF standardises in RFC 8439, at its full twenty rounds.
- Each card’s place is drawn exactly, so the shuffle favours no order of the deck.
- A fresh deal’s seed comes from your computer’s own random source, the full 64 bits, never the clock.
- Every deal is named by its number, so it can be shared and played again.
A deal has a number
The header names the deal. A fresh deal’s seed is shown in hex, in groups of four, as deal 29ed-93fe-47a3-582e; a plain number such as deal 45 is a deal too. Type either at a table and you get that deal. So you can tell a shipmate “Klondike, deal 1cec-f10e-fcc4-ee6f”, and they can play the same cards. The order seed says where the deal you are playing came from and how to come back to it, and a won or stuck deal says the same.
A seed is 64 bits, so there are 264 deals, about 1.8 × 1019: enough for a deal a minute for some 35 trillion years.
The shuffle
The deck is shuffled by Fisher and Yates’s method: each place in turn takes a card drawn from those not yet placed. With exactly uniform draws, that gives every order of the deck the same chance.
Turning a generator’s 32-bit words into a draw from, say, 52 cards is where most programs go slightly wrong. Taking the remainder, random % 52, favours some cards, because 232 is not a multiple of 52. Dog Watch builds its random library, rand, with the feature that makes each draw exact: Canon’s method, run until the carry is settled. Given uniform words from the generator, every card’s place is drawn exactly, and so is every deal.
The cipher
A random number generator in a program is almost never random. It is a small machine with a state, a rule that steps it, and a rule that reads a number out. Start it from the same seed and it gives the same numbers, which is what makes a deal replayable. A good generator’s numbers cannot be told from true randomness by any practical test; a bad one’s have patterns.
ChaCha20 is a cipher, designed so that its output cannot be told from random without its key. Cryptographers have attacked ChaCha in public since 2008. The best published attacks reach reduced versions of seven rounds, and ChaCha20 runs twenty. That is the evidence. It is not a proof, and nobody has a proof for any practical cipher.
What a seed cannot promise
- A deal you can replay is not a secret. The header shows the seed on purpose. Anyone with the number and the code can work out every face-down card. The cipher is there so the deals behave like a fair deck, not to hide them.
- 264 deals, not all of them. A deck of 52 has 52! orders, about 8.07 × 1067. Dog Watch fills ChaCha20’s 256-bit key from the 64-bit seed, so at most 264 orders can ever be dealt. The
rand_chachadocumentation says so itself: “seed_from_u64is not suitable for usage where security is important”. - The rest is headroom. Even 264 deals are far more than anyone will play.
Microsoft’s FreeCell deals
In FreeCell, deals microsoft switches to Microsoft’s 32,000 numbered deals, dealt by the generator Microsoft’s FreeCell used, reproduced to the bit and checked against the layouts players have published. deals strong switches back. Deal 11982 is the one that cannot be won.
They are there for fun, and to show the difference between a good generator and a weak one. Microsoft’s deal n came from the C library’s rand, seeded with n: a linear congruential generator that multiplies its state by 214013, adds 2531011, keeps 31 bits, and gives out only 15 of them.
- Only 32,000 deals. The deal is a function of its number.
- A small bias. A draw from 0 to 32,767, taken modulo 52, gives the first eight places 631 chances in 32,768 and the other 44 places 630. That is about 1.6 parts in a thousand, and it would be there even if the draws were perfect.
- A few cards give the deal away.
| First cards known | Deals named by them alone |
|---|---|
| 2 | 0 |
| 3 | 25,034 |
| 4 | 31,924 |
| 5 | all 32,000 |
So five cards tell you the deal, and the deal tells you every card. In FreeCell every card is face up anyway, so this costs nothing. If this generator dealt Klondike, a player who noted five cards could know every face-down card.
How other programs deal
Read from each program’s source where it is public. Microsoft’s Solitaire and the Solitaire Collection do not say how they deal.
| Program | Generator | A fresh seed from | Shuffle |
|---|---|---|---|
| Dog Watch | ChaCha20, a stream cipher | The operating system, the whole 64 bits | Fisher-Yates, exactly uniform |
| PySolFC 3.7 | Mersenne Twister; Microsoft’s for deals below 32,000 | The clock | Fisher-Yates, with a bias near n / 253 |
| PySol 4.82 | A 64-bit linear congruential generator | The clock | Fisher-Yates, with a bias near n / 231 |
| Microsoft FreeCell, Windows 3.1 to 7 | The C library’s, 15 bits of output | Not documented | A Fisher-Yates variant, with modulo bias |
| AisleRiot (GNOME) | Mersenne Twister | 128 bits from the operating system | Fisher-Yates, unbiased; deals have no numbers |
| KPatience (KDE) | Microsoft’s, for every game | The operating system | Fisher-Yates, with modulo bias |
| tty-solitaire | The C library’s rand() |
The clock, every deal | Not Fisher-Yates: it cannot be even |
| csol | The C library’s rand() |
The clock | Uniform but for modulo bias |
Of these, only Dog Watch and AisleRiot draw without bias and seed from the operating system, and AisleRiot’s deals cannot be named or shared. Only Dog Watch uses a cipher. A Mersenne Twister passes ordinary statistical tests, but anyone who sees 624 of its outputs can predict the rest.
Sources: PySolFC (github.com/shlomif/PySolFC) and pysol_cards; Debian’s copy of PySol 4.82.1; Jim Horne’s FreeCell shuffle and Michael Keller’s FreeCell FAQ at solitairelaboratory.com; AisleRiot (gitlab.gnome.org/GNOME/aisleriot) and GLib’s grand.c; KPatience (invent.kde.org/games/kpat) and Qt’s QRandomGenerator documentation; tty-solitaire (github.com/mpereira/tty-solitaire); csol (github.com/nielssp/csol).