Learn · Technologies we use
The mathematics of Secret Santa
Derangements, why the hat fails almost two times in three, why the usual fixes are unfair, one big loop, and how many draws your group has.
Pull names from a hat with 10 people and someone draws their own name 63% of the time. That's the core of Secret Santa math: a valid draw is what mathematicians call a derangement, a shuffle where nothing stays in its place, and only about 36.8% of shuffles are. The figure barely moves whether you have 6 people or 60. This page works through why, why the usual fixes quietly make the draw unfair, and how many valid draws your own group has.
Derangements, or why the hat keeps failing
Number the people 1 to n and let the hat hand each of them a slip. Any order the slips come out in is a permutation, and there are n! of them (n × (n − 1) × … × 1). A Secret Santa draw is valid only if nobody holds their own number. Count those and you get the derangement number D(n), written !n by some books:
| People | All shuffles, n! | Valid draws, D(n) | Hat fails |
|---|---|---|---|
| 3 | 6 | 2 | 66.67% |
| 4 | 24 | 9 | 62.50% |
| 5 | 120 | 44 | 63.33% |
| 6 | 720 | 265 | 63.19% |
| 8 | 40,320 | 14,833 | 63.21% |
| 10 | 3,628,800 | 1,334,961 | 63.21% |
| 20 | 2,432,902,008,176,640,000 | 895,014,631,192,902,121 | 63.21% |
The right-hand column settles on 63.21% almost at once. That number is 1 − 1/e, where e = 2.71828… is the base of natural logarithms. Inclusion–exclusion gives the exact formula. Start with all n! shuffles, subtract those where person 1 keeps their slip, and the same for everyone else, add back those where two people both do (they were subtracted twice), and keep alternating:
The sum is the start of the series for e−1, so D(n)/n! races toward 1/e. The terms shrink factorially, which is why 6 people already agree with 1/e to three decimal places. A handy consequence: D(n) is just n!/e rounded to the nearest whole number, for every n from 1 up. The neater recurrence D(n) = n·D(n − 1) + (−1)n is what our draw code uses to count exactly.
And the practical upshot: if your family redraws from scratch whenever someone gets themselves, they'll pull the slips about e ≈ 2.72 times on average. Annoying, but fair. Which is more than can be said for the shortcuts.
The fixes people use, and why they're biased
Nobody wants to redraw the whole hat, so people patch the bad draw. Both common patches give a valid result. Neither gives a fair one: some outcomes come up far more often than others. We counted every possible case exactly for 4 people (Ana, Bob, Chloe and Dev), where there are 9 valid draws and a fair method gives each one 11.11%.
"Swap with the next person." Shuffle once. Anyone holding their own name swaps slips with the next person around the table. It always ends valid, and it's badly lopsided. The draw where Ana gives to Dev, Bob to Ana, Chloe to Bob and Dev to Chloe comes up 33.33% of the time, three times its fair share, while two others come up just 4.17%. With 5 people the most likely draw is 13 times as likely as the least.
"Draw again if you pick yourself." People draw in turn, and anyone who pulls their own name puts it back and tries again. If the last person is left holding only their own name, everyone starts over (with 4 people that happens 13.89% of the time). It feels fair because each pick is random. It isn't, because early drawers have more options than late ones. With 4 people the best-treated draw is 19.35% likely and the worst 6.45%, a ratio of exactly 3. With 5 people the ratio is 4.
Does this matter at a family party? Not much. Nobody's cheating, and a skewed draw still surprises everyone. But it does matter when the point of the tool is that nobody could predict the outcome, and it's an easy bug to ship in code that "looks random". It's the same family of mistake as modulo bias, which our own passphrase generator had until we found it.
The fair method is boring and correct: make a uniformly random shuffle (Fisher–Yates with an unbiased random number), check it, and if it breaks any rule, throw the whole thing away and shuffle again. Every valid draw was exactly as likely as every other before the check, and the check keeps all of them, so they stay equally likely after. That's rejection sampling, and it's what our Secret Santa does.
How often two people draw each other
In a fair draw, the chance that at least one pair ends up buying for each other is higher than people expect: 39.3% with 10 people (525,105 of the 1,334,961 valid draws). At 4 people it's a third. A swap isn't wrong, but it spoils the guessing game at the party, because once you know who has you, you know who you have.
One big loop
Follow the gifts from anyone, Ana to Chloe, Chloe to Ed, and so on, and you'll always come back to the start. Every draw splits the group into one or more of these loops. One-big-loop mode asks for exactly one, which rules out swaps and lets you do the reveal as a circle. The number of single loops through n people is (n − 1)!: fix where Ana sits, then arrange everyone else around her.
That's a shrinking share of all valid draws as the group grows. Two thirds of draws for 4 people are already a single loop (6 of 9), but only 27.18% for 10 people (362,880 of 1,334,961) and 13.59% for 20. So "the hat happened to make one loop" gets rarer fast, and if you want it, ask for it. Our draw does this with Sattolo's variant of Fisher–Yates, which produces every single loop with equal probability, then applies the same reject-and-retry for any exclusions.
Couples, and when the rules make it impossible
Most groups have people who shouldn't draw each other: couples, housemates, a parent and the child they'd buy for anyway. Each couple removes more valid draws. With 10 people and no rules there are 1,334,961 valid draws. Make two of the pairs couples and there are 838,324. Make all five pairs couples and there are 440,192, still plenty to be unpredictable. The count comes from inclusion–exclusion again, this time over the forbidden squares on an n × n grid (the rook polynomial, if you want the search term).
Pile on enough rules and no valid draw exists. The clean way to tell is Hall's marriage theorem. Think of givers on one side and receivers on the other, with a line wherever a gift is allowed. A valid draw exists exactly when every set of givers can, between them, reach at least as many receivers as there are givers in the set. When some set can't, those people are the problem. In a group of 5 where Ana, Bob, Chloe and Dev all live together and can't draw each other, the four of them have only Ed to give to. Four givers, one receiver: impossible. Our draw finds that set with a matching algorithm and says so in plain words instead of spinning forever.
One-big-loop mode with exclusions is harder: asking whether any single loop fits is the Hamiltonian cycle problem, which has no known fast general method. At 60 people with a sensible number of exclusions, random tries find one almost at once. When they don't, the code searches exhaustively within a budget, and if the rules are so tight that even that runs out, it asks you to drop an exclusion rather than hand you a draw it can't prove was fair.
How many draws does your group have?
Exact whole-number arithmetic in your browser. Nothing is sent anywhere.
The numbers on this page come from scripts/examples/santa-math.mjs, which uses the same santa-draw.js the site runs and checks the bias figures by listing every possible shuffle.
Common questions
What is the probability that someone draws their own name in Secret Santa?
About 63% for any group of 5 or more: 1 − 1/e = 63.21%. With 3 people it's 66.67%, and with 4 it's 62.5%.
How many Secret Santa combinations are there for 10 people?
1,334,961 valid draws where nobody gets themselves, out of 3,628,800 ways to hand out the slips. With one big loop it's 362,880.
Is "redraw if you pick yourself" fair?
Each pick is random, but the overall draw isn't uniform: with 4 people some outcomes are 3 times as likely as others. Redrawing the whole hat whenever anyone gets themselves is fair.
Can a Secret Santa draw be impossible?
Yes, if exclusions leave some group of people with fewer possible recipients than there are of them. Hall's theorem says exactly when, and a matching algorithm finds who.