Hat-Check Problem

Classic Problem
The traditional wording of the question is as follows.

A hat-check girl completely loses track of which of $n$ hats belong to which owners, and hands them back at random to their $n$ owners as the latter leave.

What is the probability $p_n$ that nobody receives their own hat back?

Solution
Put into bald mathematical language, this boils down to:

For a set $S$ of $n$ elements, what is the number of derangements of $S$ divided by the number of permutations of $S$?

The answer is: approximately $\dfrac 1 e$, which can be demonstrated as follows.

Let $D_n$ be the number of derangements of a set of size $n$.

We have that:
 * The Number of Permutations of a set of size $n$ is $n!$.


 * The Closed Form for Number of Derangements on Finite Set of size $n$ is:
 * $D_n = n! \paren {1 - \dfrac 1 {1!} + \dfrac 1 {2!} - \dfrac 1 {3!} + \dotsb + \paren {-1}^n \dfrac 1 {n!} }$

So:
 * $p_n = 1 - \dfrac 1 {1!} + \dfrac 1 {2!} - \dfrac 1 {3!} + \dotsb + \left({-1}\right)^n \dfrac 1 {n!}$

Finally:
 * $1 - \dfrac 1 {1!} + \dfrac 1 {2!} - \dfrac 1 {3!} + \dotsb$

converges to $\dfrac 1 e$ by Taylor Series Expansion for Exponential Function.

Note
The accuracy of this estimate increases with increasing $n$.

Specifically, the error of the estimate is:


 * $\ds \sum_{k \mathop = 0}^{\infty} \frac {\paren {-1}^k } {k!} - \sum_{k \mathop = 0}^n \frac {\paren {-1}^k } {k!} = \sum_{k \mathop = {n + 1} }^{\infty} \frac {\paren {-1}^k } {k!}$

Therefore, as $n \to \infty$, the error goes to $0$


 * $\ds \lim_{n \mathop \to \infty} \paren {\sum_{k \mathop = 0}^{\infty} \frac {\paren {-1}^k } {k!} - \sum_{k \mathop = 0}^n \frac {\paren {-1}^k } {k!} } = 0$

Specific examples:

When $n = 3$, there are only three hats to hand back, so $p_3 = \dfrac 2 6$ which is roughly $0.0345$ (or less than $\dfrac 1 {4!}$) away from the estimate of $\dfrac 1 e$.

When $n = 8$, there are eight hats to hand back, so $p_8 = \dfrac {14\,833} {40\,320} $ which is roughly $0.0000025$ (or less than $\dfrac 1 {9!}$) away from the estimate of $\dfrac 1 e$.


 * $\begin{array}{r|rrrrrrrrrr}

n & !0 \binom n 0 & !1 \binom n 1 & !2 \binom n 2 & !3 \binom n 3 & !4 \binom n 4 & !5 \binom n 5 & !6 \binom n 6 & !7 \binom n 7 & !8 \binom n 8 \\ \hline 0 & 1 &  0 &  0 &   0 &   0 &   0 &   0 &   0 &   0  \\ 1  & 1 &  0 &  0 &   0 &   0 &   0 &   0 &   0 &   0  \\ 2  & 1 &  0 &  1 &   0 &   0 &   0 &   0 &   0 &   0  \\ 3  & 1 &  0 &  3 &   2 &   0 &   0 &   0 &   0 &   0  \\ 4  & 1 &  0 &  6 &   8 &   9 &   0 &   0 &   0 &   0  \\ 5  & 1 &  0 & 10 &  20 &  45 &  44 &   0 &   0 &   0  \\ 6  & 1 &  0 & 15 &  40 & 135 & 264 & 265 &   0 &   0  \\ 7  & 1 &  0 & 21 &  70 & 315 & 924 & 1,855 & 1,854 &   0  \\ 8  & 1 &  0 & 28 & 112 & 630 & 2464 & 7420 & 14\,832 & 14\,833  \\ \end{array}$

If the venue in question is Hilbert's Hotel, then the answer is precise.

Also known as
This problem is also known as the envelope problem, couched in the language of an incompetent secretary placing letters at random into the envelopes without checking the address on the envelope matches the addressee of the letter.