Shuffle ten names in code and, most of the time, somebody draws themselves. The exact number: 10! is 3,628,800 permutations, 1,334,961 of them have no fixed point, so 63.2% of naive shuffles assign at least one person to buy a present for themselves. Most home-rolled Secret Santa scripts are shipped with this bug still in.
I built the draw behind secretsantaname.com — free, no accounts, each person opens a private link that shows only who they drew. The form took an afternoon. The interesting work was the three rules a draw has to enforce before any link exists, and what to do when the rules contradict each other.
A shuffle is not a draw
The self-draw bug has a cheap fix: shuffle, check for fixed points, retry. A derangement of n items turns up about once per e ≈ 2.72 attempts, so rejection costs almost nothing. This is where most implementations stop, and it works until the first couple signs up.
Couples change the problem. "Ann and Ben must not draw each other" is not a fixed point. It is a forbidden pair, and it blocks in both directions. Add housemates, in-laws, two coworkers who just broke up, and the job stops being "shuffle until clean" and becomes: find a permutation that avoids an arbitrary set of forbidden assignments. Rejection sampling still works while the constraints are loose. It stops terminating when they are not.
Some rule sets have no answer
Three lines: Ann, Ben, Casey. Exclude Ann from Ben, both ways.
Ann can draw: Casey. Ben can draw: Casey. Two lines need the same single target, so no valid draw exists. The input is impossible; no algorithm fixes that. Hall's condition fails on the subset {Ann, Ben}, which has one allowed target between them.
An implementation that only retries will spin forever on this input. The honest behavior is to refuse, name the problem where possible, and leave nothing behind. The tool checks the degenerate case first, and when one person is excluded from everyone else it answers with a name: "Ann is excluded from everyone. Remove a rule." Tighter configurations fall through to the search, which reports "No valid draw is possible with these rules. Remove an exclusion." Either way the API creates nothing — no group, no links, no half-saved state — until the rules produce a complete draw.
The draw itself: backtracking, most-excluded first
The algorithm in src/logic.js, compacted:
export function draw(names, exclusions, rng) { const n = names.length; // 3..100, duplicates rejected const bad = names.map(() => new Set()); // forbidden targets per line for (const [a, b] of exclusions) { /* blocked both ways */ } const order = shuffle([...Array(n).keys()], rng) .sort((a, b) => bad[b].size - bad[a].size); // tightest lines first const res = new Array(n).fill(-1), used = new Array(n).fill(false); let steps = 0; const go = k => { if (k === n) return true; if (++steps > 200000) return false; // pathological input? stop, report const g = order[k]; for (const r of shuffle([...Array(n).keys()], rng)) { if (r === g || used[r] || bad[g].has(r)) continue; used[r] = true; res[g] = r; if (go(k + 1)) return true; used[r] = false; } res[g] = -1; return false; }; if (!go(0)) throw new Error('No valid draw is possible with these rules. Remove an exclusion.'); return res; // res[giver] = receiver index } Three decisions inside those lines.
The search order puts the most-excluded lines first, with a random shuffle as tiebreak. The tightest lines fail first, and failing early is the point of search order.
Every attempt tries targets in a fresh random order, so the draw stays random where it has room to be. The rng is a parameter: the Worker passes crypto.getRandomValues(new Uint32Array(1))[0] / 4294967296, and the tests pass a pinned sequence and assert on the outcome.
The step cap of 200,000 means a pathological input stops the search instead of burning CPU on it, and the caller gets the infeasibility message rather than a timeout.
One trade worth stating plainly. Backtracking with random order is close to, but not exactly, uniform over valid assignments — some pairings are reachable by more search paths than others. Uniform sampling over all valid matchings is a permanent-counting problem, and for a gift draw among friends nobody can measure the difference. What the backtracking buys instead is guaranteed termination and an infeasibility answer you can print.
The link is the privacy model
The pairing is half the product. The other half is who gets to see it.
Each participant's link carries a 16-character token from crypto.getRandomValues — 8 bytes in base36. The organizer token is 20 characters, the group id 12. None of them are counters or hashes of names. Sequential ids would make other people's links guessable, which ends a page titled "private" in one try.
The organizer page cannot show the pairings. Its own copy says so: "You can't see who drew whom here." There is a reveal-all button, and pressing it stamps the group with a server-side timestamp, which the page tells you before you press it. The organizer API returns no pairings and no wishlists except through that recorded reveal. Groups also expire automatically after 120 days.
Two failure modes this design rules out. A single link that decoded the entire mapping would let one leaked URL burn every pairing at once; per-person tokens cap the damage at one draw. And an organizer panel that listed everything would quietly turn "private" into decoration; here the pairings live server-side, the reveal is a recorded event, and the default view for the person who set it all up is: nothing.
Checkable by hand
Three rules: no self-draws (63.2% of raw shuffles fail this at n=10), exclusions enforced both ways (Ann, Ben, Casey with Ann and Ben excluded has no answer, and the tool says so instead of saving it), and no pairing leaves the server until a token opens exactly one assignment.
The impossible case takes about thirty seconds to reproduce at secretsantaname.com: enter those three names, exclude Ann from Ben, press Draw, and read the error. The how-it-works page documents the same behavior in plain words, including the part where no group is created until the rules produce a complete draw.
![Motion Planning and Navigation — Ch.04: Optimization Based Planning [KR]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqyrjjn7tc2h8vbe5rduk.png)
![Motion Planning and Navigation — Ch.05: Local Planners [KR]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0tuhfqde1o6zcyskw7f4.png)
![Motion Planning and Navigation — Ch.07: Multi Robot Navigation [KR]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwj9k0uan25q7slmyqpoo.png)
![Motion Planning and Navigation — Ch.10: Navigation2 Stack [KR]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6asnejhpxvcrd68qhrfg.png)