Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

The math behind a fair Secret Santa draw (and why "no couples" can make it impossible)

Every Secret Santa draw has two rules: nobody draws themselves, and nobody knows anyone else's pair. When I built a free Secret Santa generator with no email and no sign-up, I added a third rule the generator supports: "

Every Secret Santa draw has two rules: nobody draws themselves, and nobody knows anyone else's pair. When I built a free Secret Santa generator with no email and no sign-up, I added a third rule the generator supports: "don't let couples draw each other." That third rule turned out to be the interesting part.

Rule 1: nobody draws themselves

A draw is a permutation where person i gives to person p(i). "Nobody draws themselves" means no fixed points, which mathematicians call a derangement.

If you shuffle at random, the chance that you get a derangement is close to 1/e β‰ˆ 37%, almost no matter how many people there are. So "shuffle until it works" needs about 2.7 tries on average. Cheap.

Without extra rules I use Sattolo's algorithm instead, which produces a single cycle (A gives to B, B to C, ... back to A) and is a derangement by construction:

export function derange(n) {
  const a = Array.from({ length: n }, (_, i) => i);
  const r = new Uint32Array(n); crypto.getRandomValues(r);
  for (let i = n - 1; i > 0; i--) { const j = r[i] % i; [a[i], a[j]] = [a[j], a[i]]; }
  return a; // person i gives to a[i]
}

Note r[i] % i, not % (i + 1). That one character is the difference between Sattolo and Fisher–Yates.

Rule 2: nobody knows the pairs

Each person gets their own private link. Opening it shows only who they give to. The organizer's view shows names and whether each link has been opened, but never the pairs. Nothing needs an email address because the organizer shares the links themselves, usually in a WhatsApp chat, one by one.

Rule 3: "don't let couples draw each other"

Exclusions are just banned edges: if Ana and Ben are a couple, then Ana→Ben and Ben→Ana are both off limits. With a handful of banned pairs, plain rejection sampling is still fast:

export function derangeExcept(n, banned) {
  for (let t = 0; t < 20000; t++) {
    const a = shuffle(n); // Fisher–Yates with crypto.getRandomValues
    if (a.every((v, i) => v !== i && !banned.has(i + ">" + v))) return a;
  }
  return null; // tell the user this combination can't be drawn
}

The exclusions are used only to make that one draw. They are never stored.

When "no couples" makes a draw impossible

Here's the case that surprised me. Three people, one couple:

  • Ana and Ben can't draw each other, and nobody draws themselves.
  • So Ana must give to Chloe, and Ben must also give to Chloe.
  • Chloe can only receive one gift. There's no valid draw.

The same thing happens in bigger groups when one person is excluded from almost everyone. So the generator has to notice and say so ("remove a pair or add a name") instead of looping forever. Capping the attempts and returning null does that. For up to 40 names and realistic exclusion lists, a valid draw is found in a few tries, and when there's none, the cap is hit in a few milliseconds.

Small things that mattered

  • Duplicate names are rejected up front ("Sam" and "sam" are the same person in a group chat).
  • Names in the exclusion list must match the list of people, otherwise the form tells you which name it doesn't recognize.
  • Budget and exchange date are optional and shown on every person's page, so nobody has to scroll back through the chat.
  • Everything is deleted on February 1.

If you're organizing one this year, it's free: secretadvent.com/secret-santa-generator. And if you know a nicer way to pick a uniformly random derangement under exclusions than rejection sampling, I'd love to hear it in the comments.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.