Discrete Mathematics and Combinatorics

Derangements, When Nobody Gets Their Own Hat Back

Count arrangements where nothing stays in its own place, derive two recurrences and see why the number 1/e turns up.

Key takeaways

  • The first derangement numbers, starting at n = 0, are 1, 0, 1, 2, 9, 44, 265, 1,854 and 14,833.
  • Inclusion-exclusion gives Dₙ = n! × (1 − 1/1! + 1/2! − 1/3! + ... ± 1/n!).
  • Two recurrences, Dₙ = (n − 1)(Dₙ₋₁ + Dₙ₋₂) and Dₙ = n·Dₙ₋₁ + (−1)ⁿ, make the numbers quick to compute.
  • The probability that a random arrangement is a derangement tends to 1/e, about 0.3679.

A derangement is a rearrangement in which no item stays in its original place. If n guests leave hats at the door and the hats are handed back at random, the number of ways in which nobody gets their own is the n-th derangement number, written Dₙ.

The surprise is the probability. Once there are more than a handful of guests, the chance that nobody gets the right hat is very close to 36.8 percent, and it hardly matters whether there are ten guests or ten million.

Small cases by hand

With one guest, the only way to return the hat is correctly, so D₁ = 0. With two guests, swapping hats works, so D₂ = 1.

With three guests, label them 1, 2, 3 and write the hat each receives in order. The six arrangements are 123, 132, 213, 231, 312 and 321. Only 231 and 312 leave nobody matched, so D₃ = 2.

For four guests there are 24 arrangements and 9 derangements:

2143   2341   2413
3142   3412   3421
4123   4312   4321

The value D₀ = 1 may look odd. With no guests there is one arrangement, the empty one, and nobody in it has the right hat.

Counting by inclusion-exclusion

It is easier to count arrangements where particular people do get their own hats. If a chosen group of k people are matched, the other n − k hats can be arranged freely in (n − k)! ways. There are C(n, k) such groups, where C(n, k) is the number of ways to choose k of n.

The inclusion-exclusion principle then removes all arrangements with at least one match:

Dₙ = n! − C(n,1)(n−1)! + C(n,2)(n−2)! − ... 

since C(n,k)(n−k)! = n!/k!:

Dₙ = n! × (1/0! − 1/1! + 1/2! − 1/3! + ... + (−1)ⁿ/n!)

n = 4:  24 × (1 − 1 + 1/2 − 1/6 + 1/24) = 12 − 4 + 1 = 9

The result matches the list above.

A recurrence from the first guest

Here is a second route that uses no alternating sum. Guest 1 receives the hat of some other guest, say guest j. There are n − 1 choices for j. Now ask where hat 1 goes.

  • If guest j gets hat 1, the two have swapped. The remaining n − 2 guests must be deranged among themselves: Dₙ₋₂ ways.
  • If guest j does not get hat 1, treat hat 1 as the one that guest j is forbidden to receive. Now n − 1 guests each have exactly one forbidden hat: Dₙ₋₁ ways.

Therefore Dₙ = (n − 1)(Dₙ₋₁ + Dₙ₋₂) for n ≥ 2.

D₅ = 4 × (D₄ + D₃) = 4 × (9 + 2)  = 44
D₆ = 5 × (D₅ + D₄) = 5 × (44 + 9) = 265
D₇ = 6 × (265 + 44)               = 1,854
Why Dₙ = (n − 1)(Dₙ₋₁ + Dₙ₋₂)
1 Guest 1 gets hat j n − 1 possible choices of j 2 Case: a swap j takes hat 1; Dₙ₋₂ ways remain 3 Case: no swap j avoids hat 1; Dₙ₋₁ ways remain 4 Combine Add the cases, multiply by n − 1 Guest 1 gets hat j n − 1 possible choices of j 1 Case: a swap j takes hat 1; Dₙ₋₂ ways remain 2 Case: no swap j avoids hat 1; Dₙ₋₁ ways remain 3 Combine Add the cases, multiply by n − 1 4

The shorter recurrence

The sum formula gives something even simpler. Compare Dₙ with n × Dₙ₋₁. Every term of the sum up to (n − 1) agrees, and the only extra piece is the last term, n! × (−1)ⁿ/n!.

Dₙ = n × Dₙ₋₁ + (−1)ⁿ

D₈ = 8 × 1,854 + 1   = 14,833
D₉ = 9 × 14,833 − 1  = 133,496

So each derangement number is n times the previous one, nudged up or down by exactly 1. This is a first-order recurrence relation with a coefficient that changes with n.

Derangements as cycles

Any arrangement can be broken into cycles. Follow guest 1 to the owner of the hat guest 1 received, then to the owner of that person's hat, and continue until you return to guest 1. Repeat with anyone not yet visited.

A guest who gets their own hat forms a cycle of length 1. So a derangement is exactly an arrangement whose cycles all have length 2 or more.

For four guests this explains the number 9. There are two possible shapes:

one cycle of length 4      3! = 6 ways
two cycles of length 2          3 ways
total                           9

For five guests the shapes are a single 5-cycle, with 4! = 24 ways, or a 3-cycle with a 2-cycle. For the latter, choose the 3 people (10 ways) and one of the 2 directions around their cycle, giving 20. The total is 24 + 20 = 44, as the recurrences predict.

Why 1/e appears

Divide Dₙ by n!, the total number of arrangements. What remains is the partial sum 1 − 1 + 1/2! − 1/3! + ... ± 1/n!. This is the start of the series for e⁻¹, where the number e is about 2.71828.

n Dₙ n! Dₙ / n!
2 1 2 0.5000
3 2 6 0.3333
4 9 24 0.3750
5 44 120 0.3667
6 265 720 0.3681
7 1,854 5,040 0.3679

The series converges extremely fast because factorials grow so quickly. The error after n terms is smaller than 1/(n + 1)!. As a result, for every n ≥ 1, Dₙ is the whole number nearest to n!/e. For n = 4, 24/e is about 8.83, which rounds to 9.

Exactly k correct hats

What if exactly k guests get their own hats? Choose which k, then derange everyone else: C(n, k) × Dₙ₋ₖ.

For n = 4 the counts are 9 with no match, 4 × 2 = 8 with one, 6 × 1 = 6 with two, none with exactly three, and 1 with all four. They add to 24. Three matches are impossible, because the fourth hat then has only one place to go.

The average number of matches is exactly 1 for every n ≥ 1. Each guest is matched with probability 1/n, and expected values add even when events are not independent: n × 1/n = 1. See expected value.

For large n, the number of matches behaves nearly like a Poisson distribution with mean 1: the chance of exactly k matches is close to 1/(e × k!).

Two decks of cards

Shuffle two ordinary decks thoroughly and turn over one card from each at the same time, continuing through all 52 pairs. What is the chance that you never see the same card appear from both decks at once?

Relabel the cards by their positions in the first deck. The second deck is then a random arrangement of 1 to 52, and a match is a fixed point. The chance of no match is D₅₂/52!, which agrees with 1/e to far more decimal places than any experiment could detect. So you should expect at least one match about 63 percent of the time, even though each individual turn matches with probability only 1/52.

When only some people are restricted

Real problems often forbid only certain matches. Say four letters go into four addressed envelopes, one letter each, and you care only that letters A and B both miss their own envelopes.

Count all 24 arrangements, remove those with A correct (6) and those with B correct (6), then restore those with both correct (2), since they were removed twice. The answer is 24 − 12 + 2 = 14.

The opposite question is just as easy. With 5 letters, the number of ways in which at least one lands correctly is 5! − D₅ = 120 − 44 = 76, a little under two thirds of all ways.

More complicated patterns of forbidden positions can be handled the same way. Textbooks organize the bookkeeping with objects called rook polynomials, which count the ways to place non-attacking rooks on the forbidden squares of a board.

A generating function view

The whole sequence can be packed into one expression, using the exponential generating function, a power series whose coefficient of xⁿ is Dₙ/n!:

sum over n of Dₙ xⁿ / n!  =  e⁻ˣ / (1 − x)

The right side has a plain meaning. Dividing e⁻ˣ by 1 − x takes running totals of the coefficients of e⁻ˣ, which are 1, −1, 1/2, −1/6 and so on. Those running totals are exactly the partial sums in the formula for Dₙ/n!.

It can also be read backwards. Every arrangement is a set of matched guests together with a derangement of the rest, so eˣ times the series for derangements equals 1/(1 − x), the series for all arrangements. The method is introduced in generating functions.

Where derangements show up

A gift exchange in which nobody draws their own name is a derangement. If you draw names at random and start over whenever someone gets themselves, each attempt succeeds with probability near 0.37, so you need about e, roughly 2.7, attempts on average.

Redrawing only the one unlucky name is not equivalent. Patching a draw by hand can make some valid assignments more likely than others, while restarting from scratch keeps them all equally likely.

Pierre Rémond de Montmort studied the matching question in the early eighteenth century, in connection with a card game. Members of the Bernoulli family and later Euler also worked on it.

Matching problems also serve as baselines. If someone pairs 10 baby photos with 10 adults and gets several right, comparing with random matching, which averages 1 correct, shows whether the result is better than guessing.

Common mistakes

One mistake is to reason that each guest misses with probability (n − 1)/n, so everyone misses with probability ((n − 1)/n)ⁿ. The events are not independent. The expression happens to tend to 1/e too, but it is wrong for finite n: for n = 2 it gives 1/4, while the truth is 1/2.

Another is to expect the probability to head toward 0 or 1 as the party grows. More guests mean more chances for someone to be matched, but each particular match becomes less likely, and the two effects balance.

A third is forgetting that this assumes every arrangement is equally likely. A distracted attendant who tends to hand back hats in nearly the right order would produce very different numbers.

Checks you can make yourself

The recurrences reveal patterns that are handy for catching arithmetic slips. Since Dₙ = (n − 1)(Dₙ₋₁ + Dₙ₋₂), each Dₙ with n ≥ 2 is divisible by n − 1. For example, 1,854 = 6 × 309 and 14,833 = 7 × 2,119.

Since Dₙ = n·Dₙ₋₁ + (−1)ⁿ, each Dₙ with n ≥ 1 leaves a remainder of 1 or n − 1 on division by n, according to whether n is even or odd.

Try extending the table to n = 10. Multiply D₉ = 133,496 by 10 and add 1 to get 1,334,961. Then confirm it with the other recurrence: 9 × (133,496 + 14,833) = 9 × 148,329 = 1,334,961. Agreement between two rules is a convincing sign that both calculations are right.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic