The guide
Nobody gets their own hat: why the odds settle at one in e
A derangement is an arrangement in which no item lands in its own place. Three different formulas count them, and for a crowd of almost any size the chance sits near 0.37.
- 9 of 24 ways to hand four tests back so nobody gets their own
- 11/30 chance nobody gets their own hat, five hats
- 264 permutations of six items with exactly one fixed point
- 0.367879 the limit of that chance, one over e
- 176,214,841 derangements of twelve items
Read the full guide Fold the guide
A professor has four tests to hand back and wants the students to grade each other’s work. Call the students A, B, C and D. There are 24 ways to hand the tests out, and the answer is 9 of 24: only in those 9 does nobody end up holding their own paper. The Wikipedia article on derangements uses exactly this picture, and it is the quickest way into the subject.
A derangement is a permutation of a set in which no element appears in its original position. Put the other way round, it is a permutation with no fixed points, and the article on permutations defines a fixed point as an element that is taken to itself. The article calls the count of derangements of n items the derangement number, the subfactorial or the de Montmort number, and writes it D(n) or !n.
Small values come fast, and OEIS entry A000166 lists them. D(0) is 1, D(1) is 0, then 1, 2, 9, 44, 265 and 1,854. By D(12) the count has reached 176,214,841, and the table in the article stops soon after at 2,290,792,932 for 13 items.
A problem older than its notation
The question was first considered by Pierre Remond de Montmort in his Essay d’analyse sur les jeux de hazard, which appeared in 1708. According to the article he solved it in 1713, and Nicolaus Bernoulli solved it at about the same time. The MacTutor biography dates Montmort from 1678 to 1719 and says the 1713 second edition carried their correspondence, which had begun in 1710.
It is usually told as the hat-check problem. A number of guests hand in their hats, the hats come back at random, and someone asks how many ways there are for no guest to get their own hat back. The article on the constant e tells the same story with a butler and boxes, and credits Jacob Bernoulli with a part in the discovery. The article on random permutation statistics tells it with umbrellas at a party and finds the chance of no match to be 1 / e.
Three ways to the same number
The first route is a recurrence. Take the first guest. The hat they receive belongs to someone else, say guest i, and then there are two cases. Either guest i receives the first guest’s hat, which leaves a smaller problem of n minus 2 guests, or guest i receives something else, which is equivalent to a problem of n minus 1. Each of the n minus 1 possible hats gives the sum of those two counts, so D(n) is (n - 1) times (D(n - 1) + D(n - 2)).
Run it upward from D(0) = 1 and D(1) = 0. D(2) is 1 times (0 + 1), which is 1. D(3) is 2 times (1 + 0), which is 2. D(4) is 3 times (2 + 1), which is 9. D(5) is 4 times (9 + 2), which is 44, and D(6) is 5 times (44 + 9), which is 265.
The second route comes from inclusion and exclusion, which the article on that principle also works through with a shuffled deck of numbered cards. Let one set hold the permutations that fix the first object, another those that fix the second, and so on. Any i of these sets overlap in the permutations that fix those i objects and shuffle the rest freely, which leaves (n - i)! of them, and there are C(n, i) ways to choose the i. Alternating the counts and subtracting the total from n! gives D(n) as n! times the sum of (-1)^i / i! for i from 0 to n.
On paper that looks like a sum of fractions. The tool avoids fractions: each term n! / i! is a whole number, so the sum is exact. For four items it reads 24 - 24 + 12 - 4 + 1, which is 9. For five it is 120 - 120 + 60 - 20 + 5 - 1, which is 44.
The third route looks like a trick. For n of 1 or more, D(n) is the nearest integer to n! divided by e. Take 24 / e, which is 8.83, and the nearest whole number is 9. Take 120 / e, which is 44.15, and you get 44. The page works this out with e held as an exact fixed-point integer to as many places as n! has digits plus 30 more, and it prints that number of places.
The word that matters is nearest. The article on rencontres numbers notes that n! / e has to be rounded up for even n and down for odd n. A quick answer that rounds down always is wrong for every even n: 720 / e is 264.87, rounding down gives 264, and D(6) is 265.
Hurried solvers make another slip. The article lists a quick recurrence, D(n) = n D(n - 1) + (-1)^n, and dropping its last term gives 8 for D(4) and 45 for D(5), against the true 9 and 44. With the term kept it checks: 4 times 2, plus 1, is 9, and 5 times 9, minus 1, is 44.
Why the odds settle at one in e
Divide the alternating sum by n! and what is left is the series 1 - 1/1! + 1/2! - 1/3! and so on. The series for e to the power x is the sum of x^i / i!, and putting x = -1 shows that D(n) / n! tends to 1 / e, about 0.367879. The article adds that the convergence is extremely quick, which is why rounding n! / e lands on the right integer.
Look at the numbers. For five hats the chance of no match is 44 / 120, which reduces to 11/30, or 0.36667. For seven items it is 0.367857. For ten it is 1,334,961 over 3,628,800, which is 0.36787946, set beside 1 / e at 0.36787944. Ten items already agree with the limit to seven decimal places.
The size of the crowd hardly matters. For 1,000 items the chance is 0.367879441171 to twelve places. D(1000) has 2,568 digits, and 1,000! has the same number. The tool shows the gap from 1 / e, about 2.48 times ten to the minus 2,571, beside the bound 1 / (n + 1)!, which is a hair larger.
That bound is not quoted from any of the sources. It comes from the alternating series, whose terms shrink, and it was checked rather than assumed: for every n from 0 to 399 the gap is smaller than 1 / (n + 1)!, using e computed to more digits than n! has.
Exactly k fixed points
A different question asks how many permutations have exactly k items in their own place. The article on rencontres numbers, and OEIS entry A008290 with its triangle of counts, give a vivid case: seven presents given to seven people, with exactly two of them landing correctly, can happen in 924 ways. The French name means an encounter.
The count has two parts. Choose which k items stay put, which can be done in C(n, k) ways. Then derange the other n minus k, which can be done in D(n - k) ways. Multiply them. The binomial factor is the part that is forgotten most often. For six items with exactly one fixed point the right answer is 6 times 44, which is 264. Leave out the factor and you would report 44.
Here is a worked example you can reproduce in the tool: enter n = 6 and k = 1. The whole row for six items, from k = 0 to k = 6, is 265, 264, 135, 40, 15, 0 and 1. The zero is not a typing error. If five of six items are in place, the sixth has nowhere else to be, so exactly five fixed points cannot happen. Every row adds up to n!, and here the six counts add up to 720.
For large n the picture simplifies. The article says the expected number of fixed points of a random permutation is 1, and that the chance of exactly k fixed points tends to e^-1 divided by k!. So for a big crowd, exactly zero and exactly one match are equally likely, each near 0.37.
What the tool does not do
The calculator is exact but narrow. It works for n up to 1,000 and lists the rencontres table for rows up to n = 200. It lists every derangement for n up to 8, in lexicographic order, because that means searching all 40,320 permutations, of which 14,833 are derangements, and it checks the formulas against that search.
It does not handle lists with repeated items, where the article says the answer involves Laguerre polynomials. It does not solve the menage problem, which seats couples around a table so that nobody sits next to their partner, and it proves nothing. Those are separate questions. For plain counts of ordered or unordered selections, the permutation and combination calculator is the better tool.
The arithmetic uses the language’s built-in arbitrary-size integers, and the long sums run in a background worker so the page stays responsive. Nothing typed here leaves the device.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Derangement - Wikipedia Wikipedia (CC BY-SA 4.0)
- Rencontres numbers - Wikipedia Wikipedia (CC BY-SA 4.0)
- Permutation - Wikipedia Wikipedia (CC BY-SA 4.0)
- Inclusion-exclusion principle - Wikipedia Wikipedia (CC BY-SA 4.0)
- Random permutation statistics - Wikipedia Wikipedia (CC BY-SA 4.0)
- e (mathematical constant) - Wikipedia Wikipedia (CC BY-SA 4.0)
- A000166 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A008290 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- Pierre Remond de Montmort (1678 - 1719) - Biography MacTutor History of Mathematics, University of St Andrews (CC BY-SA 4.0)
- BigInt - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- Web Workers API - Web APIs | MDN MDN Web Docs (CC BY-SA 2.5 or later)