Derangements
At a party, guests check their hats. They receive them back at random. What is the probability that not a single guest gets their own hat back? This whimsical question — the classical hatcheck problem — is the birthplace of one of the most elegant results in enumerative combinatorics: the theory of derangements. A derangement is a permutation of a finite set that moves every element away from its original position. It leaves no point fixed. The problem matters because it is the cleanest nontrivial application of the Principle of Inclusion-Exclusion. This is the workhorse tool for counting "none of these bad events happen." Counting derangements forces us to organize an alternating sum over all subsets of "bad positions." The computation collapses beautifully into a truncated Taylor series for . This is no coincidence. It is the first hint, within elementary combinatorics, of the deep bridge between counting problems and analytic functions. That bridge later matures into the theory of Exponential Generating Functions. The big picture is this: derangements sit at the intersection of exact enumeration and asymptotic analysis. On one hand, we get an exact closed formula, a clean recurrence. We even get a strikingly simple statement — the nearest integer to . On the other hand, as the fraction of permutations that are derangements converges to . This is a constant appearing nowhere in the original combinatorial setup except through the inclusion-exclusion alternating series. Few results package "exact" and "asymptotic" so tightly into one number. This article develops the theory from first principles. It covers the rigorous definition of a derangement, and a full proof of the counting formula via inclusion-exclusion. It also gives a two-term recurrence proof by a direct combinatorial bijection. We present the limit theorem with a complete proof, worked numerical examples. We also discuss where the hypotheses genuinely matter. We close by connecting derangements to their exponential generating function, the natural next step in this subject.
- A derangement of is a permutation with no fixed points: for every .
- The count satisfies the exact formula , proved by Principle of Inclusion-Exclusion.
- obeys the clean recurrences and .
- As , the probability that a random permutation is a derangement converges to .
- For every , equals the nearest integer to .
- Key idea: counting "no fixed points" by alternating over "at least these points are fixed" turns a hard combinatorial constraint into a truncated exponential series.
Where this lives today
Derangements are the archetypal example motivating Exponential Generating Functions: the exponential generating function
turns the alternating-sum formula into a single closed rational-times-exponential expression, and manipulating this generating function reproduces both the recurrences proved above via coefficient extraction — a template used throughout enumerative combinatorics for permutation statistics. In probability and statistics, derangements underlie the analysis of matching problems: testing whether observed rankings correlate with true rankings (e.g., in nonparametric statistics, the number of "no-match" permutations calibrates rank-correlation tests). In cryptography and coding theory, fixed-point-free permutations arise in the design of certain block ciphers and permutation-based hash constructions, where guaranteeing no element maps to itself is a security requirement. In computer science, derangements appear in the analysis of hashing and shuffling algorithms, and as a classic case study for randomized algorithms verifying properties of random permutations (e.g., cycle structure algorithms, Fisher–Yates shuffle variants). In card-shuffling and recreational mathematics, they explain the "Secret Santa" gift-exchange problem: the probability that a random assignment gives no one their own name approaches for large groups, exactly . Finally, the constant recurs across combinatorics wherever inclusion-exclusion is applied to "none of independent-ish bad events occur," making derangements the cleanest gateway example for a phenomenon seen again in the study of Permutations with more general forbidden-position patterns (the "ménage problem" and rook polynomials on forbidden boards).
Ask the tutor: How does the exponential generating function actually reproduce the recurrence by coefficient extraction?
Let be a positive integer and let denote the symmetric group of all permutations of the set . A permutation is called a derangement if it has no fixed points, that is,
The derangement number (also written , subfactorial ) is the number of derangements in :
By convention (the empty permutation of the empty set vacuously has no fixed points).
Intuition
Think of as an assignment of labeled hats back to labeled owners. A derangement is exactly an assignment in which nobody, ever, gets their own hat. The total number of permutations is ; we want to subtract off all the "bad" ones — those with at least one fixed point — but a permutation can be bad in many overlapping ways (fixing point , or point , or both, etc.), so naive subtraction over-corrects. This overlap is precisely what Principle of Inclusion-Exclusion is built to handle: alternately subtract and add back counts of permutations fixing prescribed subsets of points, and the terms self-organize into the partial sums of the power series for . The picture below shows the finite alternating sum for visibly converging toward , landing exactly on the integer after rounding — a preview of the "nearest integer to " theorem proved later.
The oscillation dies out fast: after just five terms the partial sum already lands within of the limiting value , close enough that rounding to the nearest integer recovers exactly. This rapid convergence — inherited from the alternating factorial series — is what makes the "nearest integer" theorem work for every , not just large .
Let be an integer, and let be the number of derangements of (with ). Then
We use the Principle of Inclusion-Exclusion (Principle of Inclusion-Exclusion).
Setup. Work inside the universe of all permutations of . For each , let
be the set of permutations fixing . A permutation is a derangement exactly when it lies in none of the , so
Computing intersections. Fix a subset with . We claim
Indeed, a permutation lying in must satisfy for every ; on the remaining elements of , may act as any bijection of that set to itself (this does not disturb the fixed points in , since restricted to is a bijection — it must map into because every point of is already used as an image by the identity on , and is a bijection of the whole set ). There are exactly such bijections, proving the claim.
Applying inclusion-exclusion. The Principle of Inclusion-Exclusion states that for events (subsets) of a finite universe,
Grouping the sum by , there are subsets of size , each contributing by the claim above. Hence
Simplifying. Since , this becomes
Therefore
using for the term. This is exactly the claimed formula. The case is handled by convention (), and the argument above is valid for every integer since it used no property of beyond finiteness.
∎For every integer ,
We give a direct combinatorial (bijective) proof, not relying on the closed formula above, so as to give an independent verification. Let be a derangement of . Consider the value ; since is a derangement, for some (that is, ), giving choices for . Fix such a . We split into two cases based on whether .
Case A: . Then swaps and . Removing both and from consideration, the restriction of to (a set of size ) is itself a bijection of that set to itself, and it must have no fixed points, because any fixed point there is a fixed point of . Conversely, given any derangement of the -element set , we get a derangement of by setting , , and elsewhere. This is a bijection between such and derangements of an -set, so there are exactly permutations in Case A (for this fixed ).
Case B: . Define on (an -element set) by
This is well-defined as a map : for , would need checking — indeed is impossible unless since means 's preimage is (as is a bijection, the unique preimage of under is ). So for , , hence automatically excludes ; and we also need for : this holds because (as is a derangement). Also . So is a derangement of the -set . This construction is reversible: given a derangement of and a value , we recover by , ... Let us restate the reverse map cleanly: given any derangement of -sized set (which necessarily has for no , since maps into — rather is a derangement of that set meaning , values ranging over too, so never outputs ). To invert Case B's construction: given , a derangement of , and given the chosen , define , , and for . One checks is a derangement of : ; ; for , since is a derangement. This satisfies 's original role... more precisely this exactly matches "Case B" since here , wait — that contradicts Case B's defining condition . Let us redo this cleanly with correct case bookkeeping. Fix . Case B is . Define by for , and is not used; instead set is wrong since already (as in Case B) — so actually restricted to almost works as a map to except that the value receives image from the "missing" element , i.e. tells us 's preimage is , not that maps somewhere disallowed. The correct fix-up is: define by for all , except redefine nothing — we must handle that could equal anything in (Case B excludes , and always). Since maps bijectively onto (because is the unique preimage of ), the map is not a self-map of — its image misses and includes instead (from , only if , excluded in Case B) — actually since in Case B, and , the image of is , which still doesn't include 's slot correctly. To fix this, define on by: for , and ... but . We resolve this the standard way: relabel as within the image. Define by
The clean, standard construction is: define by merging the roles of and . Since and (Case B), define
This is a self-map of : for (), never equals (its only preimage is itself, not ), so ; and by Case B's hypothesis together with automatically. Injectivity of on follows from injectivity of on restricted to this domain (the only collision risk, values equal to , cannot occur as just shown). Also for all in the domain: for , since is a derangement; for , , again since is a derangement. So is a derangement of the -set . This construction is invertible: given a derangement of and a chosen , define , , and for . Then is a permutation of (bijectivity checks directly), it is a derangement (same checks as above, reversed), , and automatically since takes values in — so this lands exactly in Case B. Hence Case B permutations (for fixed ) are in bijection with derangements of an -element set, giving of them.
Total count. For each of the choices of , there are derangements of type Case A and of type Case B, and every derangement falls into exactly one case for its own value of . Summing,
For every integer , , with .
By induction on , using the closed formula (proved above; this is a direct algebraic manipulation, not a circular reuse of the recurrence itself). Compute:
Then
using for the middle step. The base case : (indeed the only permutation of is the identity, which fixes , so ), confirming consistency.
∎Let denote the probability that a uniformly random permutation of is a derangement. Then
Moreover for every , is the nearest integer to ; that is,
Part 1: the limit. By the counting formula,
This is precisely the -th partial sum of the Taylor series
which converges absolutely (by the ratio test: ) to , since the exponential function has infinite radius of convergence and this is its value at . Absolute (indeed unconditional) convergence of the series to means exactly that the sequence of partial sums converges to :
Part 2: nearest integer. We must show for all , which implies is the unique nearest integer to (note is irrational for every since is irrational and is a nonzero rational integer, so there is never a tie at exactly ). The tail of the alternating series for satisfies, by the alternating series remainder bound (valid since is decreasing to ),
(This is the standard alternating series estimate: for a series with , the error after truncating at term is bounded by the first omitted term ; here .) Multiplying through by :
For , , with equality only if . We must check separately since the bound alone gives , not strictly less. Direct computation: and , so . For , strictly, so the bound already gives strict inequality . Hence for every , , so is the unique integer within distance of , i.e. the nearest integer:
Worked examples
Example 1: computing two independent ways. Via the closed formula. We need multiplied by . Compute term by term:
Using a common denominator of : the terms are (each is with the appropriate sign). Summing:
So . Via the recurrence , checking against known small values. We build up from :
- .
- .
- .
- .
- .
- .
- .
This gives , not — so one of the computations has an arithmetic slip; let us recheck the closed-formula sum, since the recurrence derivation was proven algebraically from the formula and both must agree. Rechecking: , , , , , , , . With alternating signs starting at : . Step by step: ; ; ; ; ; ; . The correct sum is , matching the recurrence exactly. (The earlier partial addition "1894" was an arithmetic error in adding as versus mis-stepping later — corrected above.) So we confirm:
As a sanity check against the nearest-integer theorem: , and the nearest integer to is indeed . All three methods agree.
Example 2: a conditional derangement-type count — permutations of avoiding a prescribed set of positions. Suppose we want the number of permutations of such that , , and , but with no constraint on positions (they are free to be fixed or not). This is a generalization of the derangement count — a partial derangement — and we compute it directly by inclusion-exclusion, mirroring the derangement proof but only over the sub-collection instead of all . Let for . We want . For any with , by the same argument as in the main proof (fix points, permute the rest freely). By inclusion-exclusion,
So the desired count is
As a check via the complementary formula for partial derangements (avoiding exactly prescribed points out of ): the general formula is
more simply, the count of permutations of avoiding prescribed fixed points equals . Here :
matching the direct computation. This illustrates that full derangements () are the special case of this more general partial-avoidance count, and the same inclusion-exclusion machinery handles both.
The theorem's setting is permutations of the finite set . If we drop finiteness — say, we ask about "derangements" of the countably infinite set , meaning bijections with for all — several things break. First, there is no finite total to normalize against, so the very statement "" is meaningless: there is no natural uniform probability measure on the infinite symmetric group analogous to the uniform measure on the finite (any attempt to define "uniform measure" on a countably infinite set conflicts with countable additivity — no probability measure can assign equal positive mass to infinitely many disjoint outcomes, and assigning zero mass to each makes all events measure zero). Second, the inclusion-exclusion proof itself breaks down: it relied on summing over finite subsets and used the finite formula , which used the finiteness of to conclude there are bijections of the complement. For the infinite set (still countably infinite for any finite ), the number of bijections of to itself is not in any sensible numerical sense — it is an infinite cardinal (indeed many bijections), so the inclusion-exclusion sum cannot even be formed as a finite or convergent numerical series in the same way. Concretely, infinite fixed-point-free bijections of certainly exist (e.g. pairs up consecutive integers with no fixed points), but there is no meaningful sense in which "almost all" bijections of are fixed-point-free with limiting frequency , because there is no finite reference count to take a ratio against. This shows the finiteness hypothesis on the ground set is not a technicality — it is what makes , the ratio , and the entire inclusion-exclusion computation meaningful in the first place.
Scope, edge cases & common traps
| Situation | What holds | What can go wrong |
|---|---|---|
| by convention (empty permutation) | Forgetting this convention breaks the recurrence's base case | |
| (identity is the only permutation, and it fixes the point) | Assuming is a common off-by-one slip | |
| general | = nearest integer to | Forgetting the bound needs to guarantee strict inequality at |
| Partial avoidance (only positions banned) | Use , a genuinely different, larger count than | Confusing "avoid specific points" with "derangement of all points" |
| Infinite ground set | No analogue of | Misapplying the finite- asymptotic to infinite permutation groups |
The mathematician behind it
Origins · 1 minPierre Rémond de Montmort and how this was discovered
Montmort was a French mathematician who studied games of chance and posed the "problem of coincidences" (later popularized as the hatcheck problem) in his 1708 treatise on the analysis of games of chance, Essai d'analyse sur les jeux de hazard. He corresponded extensively with Nicolaus (I) Bernoulli, who contributed to solving the derangement-counting problem, and their exchange is among the earliest rigorous treatments of what we now call inclusion-exclusion counting.
Why is and not something else?
The only permutation of a single-element set is the identity map , since there is exactly one bijection from a one-element set to itself. This identity necessarily fixes the point , so it is not a derangement. Since contains only this one permutation and it fails the no-fixed-point condition, there are zero derangements of a single element: . This matches the formula and the recurrence , so all methods agree.
Why does the alternating sum appear instead of a simpler sum?
The alternating sign comes directly from the Principle of Inclusion-Exclusion, which alternately adds and subtracts overlapping counts to avoid double-counting permutations that fix multiple points simultaneously. Each term (after simplification, ) corresponds to correcting for permutations fixing exactly a -subset of points, counted with multiplicity according to how many larger subsets contain them. The alternating structure is what makes the infinite series it approximates, , converge — a non-alternating sum of terms would just converge to , an entirely different (and combinatorially meaningless here) constant.
Is every derangement necessarily a product of disjoint transpositions, like the hat-swapping picture suggests?
No. A derangement can have cycles of any length from up to , as long as no cycle has length (a length- cycle would be a fixed point). For example, the -cycle on , meaning , is a derangement with a single -cycle and no transpositions at all. The hat-swapping intuition (pairwise swaps) illustrates only the special case of derangements built entirely from -cycles, which requires even; general derangements decompose into disjoint cycles of length of arbitrary lengths summing to .
How fast does converge to , and does the sign of the error alternate?
The convergence is extremely fast: the error satisfies , which shrinks faster than exponentially. The sign of the error alternates with because the underlying series for is alternating: overshoots for even and undershoots it for odd (or vice versa depending on convention for the first term), a direct consequence of the alternating series estimation theorem used in the asymptotic theorem's proof. In practice, by the value agrees with to about eight decimal places.
Can the derangement formula be derived without inclusion-exclusion, e.g. purely by the recurrence?
Yes, in principle: one can define via the combinatorial recurrence proved bijectively above, together with base cases , and then solve this linear recurrence with variable coefficients (e.g. by generating functions) to rederive the closed formula. This avoids restating inclusion-exclusion but is not really independent of it in spirit, since the standard route to solving the recurrence goes through the exponential generating function , which itself packages the same alternating-sum structure that inclusion-exclusion produces directly.
What is the exponential generating function of , and how does it foreshadow later material?
The exponential generating function is . This single closed form encodes the entire sequence and makes both recurrences and the closed formula derivable by standard generating-function manipulations (multiplying power series, extracting coefficients). It is a first, concrete example of how the abstract machinery of Exponential Generating Functions converts a combinatorial counting problem with an inclusion-exclusion flavor into an algebraic manipulation of formal power series, a technique that generalizes far beyond derangements to permutation statistics, set partitions, and labeled combinatorial structures.
Does the derangement count relate to the number of ways to arrange non-attacking rooks avoiding the diagonal?
Yes: equals the number of placements of non-attacking rooks on an chessboard such that no rook sits on a diagonal square , since a permutation matrix corresponds exactly to a non-attacking rook placement, and avoiding the diagonal corresponds exactly to having no fixed point. This rook-polynomial viewpoint generalizes derangements to arbitrary "forbidden boards," connecting the topic to the broader theory of permutations with restricted positions and to combinatorial designs like the ménage problem.