← Nesim Apps

Secret Santa Draw Algorithm: No Self-Picks, No Swaps

By Nesim Razon, independent mobile developer · 2026-11-23

#Algorithms#Dev notes

A Secret Santa draw sounds trivial: everyone picks a name. Then someone picks their own, two people end up giving to each other, and a couple draws each other when they wanted to surprise someone else. This post explains the algorithm behind my free draw page: build one big cycle, respect exclusion pairs, fall back when no cycle exists, and prove the result with tests.

The naive draw and where it breaks

The first idea is a hat: each person in turn takes a random name from the hat that is not their own. It fails in a quiet way. When the last person reaches the hat, the only name left can be their own. In a simulation of 100,000 draws with 10 people, about 7 or 8 in 100 got stuck like this.

The second idea is to shuffle all names, check that nobody got themselves, and shuffle again if someone did. That always ends with a valid result, but it wastes work. A shuffle with no self-pick is called a derangement, and for 10 people it took 2.7 tries on average in the same simulation.

A valid shuffle can still contain swaps

Take 4 people. There are 24 ways to assign gifts. Only 9 have no self-pick. Look at those 9:

So a third of the valid results for 4 people are swaps. With 10 people, about 4 in 10 of the valid shuffles contained at least one swap in my simulation. A swap is not wrong, but it is dull: you learn who your gift-giver is the moment you pick. Most groups want it avoided.

One big cycle: shuffle and hand to the neighbor

The fix is to stop thinking of a table of picks and think of a ring. Shuffle the names into a line, then let each person give to the next one in the line, and the last to the first. In the code this is one line:

res[cycle[i]] = cycle[(i + 1) % n]

Four people shuffled to C, A, D, B give C to A, A to D, D to B and B to C. Everyone gives once, everyone receives once, nobody gives to themselves. For more than two people, nobody can swap either, because a swap would need two people pointing at each other, and in one ring each person points only to the next.

The count matches the idea: there are (n - 1)! distinct rings. That is 2 for 3 people, 6 for 4 people and 362,880 for 10 people, plenty of variety.

The shuffle underneath: Fisher-Yates

Everything above depends on a fair shuffle. The code uses the Fisher-Yates method: walk from the last position down to the second, pick a random position from the start up to the current one, and swap the two. When the random numbers are good, each of the n! orders is equally likely. The loop in short:

for (i = n - 1; i > 0; i--) { j = floor(rnd() * (i + 1)); swap(a[i], a[j]); }

A common mistake is sorting with a random comparison, which does not give every order the same chance. Another detail: the function shuffles a copy, so your original list of names is never changed. The random source is a parameter that defaults to Math.random, so a test could pass a fixed sequence and repeat a draw exactly.

Fisher-Yates makes a plain ring fair. With exclusion pairs, the search takes the first ring the backtracking finds from a random start, and I do not claim that every valid ring is equally likely. For a gift draw that is fine. The whole thing runs in the page, and no names are sent to any server.

Adding rules: couples and exclusion pairs

Real groups have rules: partners should not draw each other, or two coworkers who already exchange gifts. The draw page stores each rule as a pair and writes it into a ban table in both directions, so one pair blocks A giving to B and B giving to A. Every name also bans itself, which is how self-picks are removed.

Now a plain shuffle is not enough, because a random neighbor may be banned. The code builds the ring step by step: start from the first name in a shuffled list, try the remaining names in random order, skip anyone already used or banned, and if the chain gets stuck, step back and try another name. This is called backtracking. When the ring is full, the last person must also be allowed to give to the first.

Example with 4 people where A and B must not draw each other. Of the 6 rings, only 2 keep A and B apart: A to C to B to D to A, and A to D to B to C to A. The code finds one of them within a few steps.

The fallback and the impossible case

Sometimes no ring exists but a valid assignment does. Take 4 people with exclusions A-B, B-C, C-D and D-A. A can give only to C, B only to D, and so on, so the only answer is A and C swapping and B and D swapping. No ring can do that.

So the code works in two stages. First it makes up to 60 attempts at a ring, each with a budget of 4,000 steps. If none works, it runs a plain random backtracking assignment with a budget of 50,000 steps. The result says which one it was: cycle true or cycle false. The page can then tell the group that swaps were unavoidable.

If both stages fail, the function returns null. With 3 people where A is excluded with both B and C, nobody can give to A and A can give to nobody, so null is the right answer. Note that null also means not found within the budget, so for very large and very tight rule sets it is a practical limit, not a proof. With 2 people the only result is a swap; the helper that checks feasibility returns false for fewer than 3 names.

How I know it is correct

The guarantees are simple to state. Everyone gives once and receives once. Nobody gets themselves. No excluded pair appears in either direction. When the result says cycle true, walking from the first name returns to it after exactly n steps.

The test file draws 400 times for each of four groups: 3 people, 4 people with one exclusion, 10 people with three couples and 50 people with two exclusion pairs. After every draw it checks four things: the receiver exists and is not the giver, no receiver appears twice, no excluded pair appears, and for ring results the walk takes exactly n steps. One more test checks that the impossible 3-person case returns null.

If you only want to run a draw, the page is at nesimapps.com/cekilis/ and the user side is covered in the post on running an online Secret Santa draw with no sign-up.

Frequently asked questions

Why does picking names from a hat sometimes get stuck?

The last person can be left with only their own name. In a 10-person simulation this happened in about 7 to 8 of 100 draws.

What is a derangement?

A shuffle in which nobody ends up in their original place, so nobody draws themselves. It can still contain two people giving to each other.

How does one big cycle avoid mutual swaps?

In one ring each person gives only to the next person in the ring, so two people cannot point at each other when there are more than two people.

What if the exclusion rules make a ring impossible?

The algorithm falls back to a general backtracking assignment, where swaps may appear, and returns nothing if that also fails.

Do names need to be unique?

Yes. The function uses each name as a key in its tables, so two identical names would be treated as one person.

More from the blog