# Derangements: A Riddle About Misallocated Hats

### 2 January 2019

• Keywords:
• code-for-novices
• simulation

Shiri Avni

This post is aimed at showing non-programmers or novice progammers how knowing a little bid of code can help you tackle interesting riddles.

# Riddle Overview

There is a classic hats riddle that goes like this:

$N$ people attend a party, and they each bring a single hat with them. When they enter the party, they leave their hat at the reception. At the end of the party, the $N$ guests go to pick up their hats, but the receptionist was tired/drunk/mischievous and gave each person a random hat instead. What is the probability that none of the guests receive their original hat back?

Take a moment to try to think of the answer. To illustrate the dependencies of the probabilities between guests, let’s consider a small party with guests Anna, Bob, Carla, and Dan ($N=4$).

Let’s assume that Anna picks up a hat from the reception first. The probability that Anna does not receive her original hat is $\frac{N-1}{N}$, which in this case is $\frac{4-1}{4}=\frac{3}{4}$. Let’s say that Anna receives Bob’s hat, so the hats left at the reception are now Anna’s, Carla’s, and Dan’s.

If Bob is the second guest to pick up a hat, then the probability that Bob does not receive his original hat is $\frac{N-1}{N-1}=\frac{3}{3}=1$, because all the remaining hats at the reception are not his. On the other hand, if Carla is the second person to pick up a hat, then since Bob’s hat is already taken, then Carla only has a $\frac{2}{3}$ chance of picking a hat that is not hers. So as we an see, there are dependencies between the guests and this complicates the solution.

# Analytic Solution

The combinatorial solution to this problem is interesting, and you are welcome to read up on it here. The striking result is that as ${N\to\infty}$, the probability that no person receives his/her own back approaches a constant value of $\frac{1}{e}$. This is not at all obvious, and I would have initially guessed that the final expression for this probability involves $N$ in such a way that it cannot be factored out.

# Simulation

What I would like to do in the rest of this post is illustrate how coding can quickly help you approach problems such as these even if you don’t know the necessary math to solve the problem directly. Namely, we will take a “large” value of $N$ and simulate the reception problem $K$ times. By keeping track of how many times we obtain a valid solution (in which no guest receives his/her own hat), we can approximate the probability as $\frac{num\_valid\_solutions}{K}$.

From this point onwards, I will assume that you are familiar with basic Python and NumPy arrays.

We will label the guests $0...(N-1)$ and create an array $A$ such that $A[i]=j$ if the $i$-th guest receives the $j$-th’s guest’s hat in return. In this way, valid solutions are those such that $A[i]!=i$ for all $i$.

import numpy as np

num_guests = 10000
num_trials = 10000

num_successful_trials = 0
helper_arr = np.arange(num_guests) #in this array, a[i]=i for all i

for i in range(num_trials):
trial = np.random.permutation(num_guests)
if np.sum(helper_arr == trial) == 0: #no guest receives hat back
num_successful_trials+=1

probability = round(num_successful_trials/num_trials, 5) #round to 5 digits
print("Probability: ", probability)


The probability value we get changes between runs, but the value satisfies $0.36^{\star\star}$ for all the runs I tried. Considering that $\frac{1}{e} \approx 0.3678$, we’re quite close!

# Conclusion

In many cases, simulating mathematical models can help you get some intuition about the qualities of a solution. In our case here, changing the code’s parameters to larger values of $N$ (e.g. 50,000) still results in similar probabilities, which provides evidence for the limiting probability not being dependent on $N$ at all.

In other cases in which you may have come up with a numeric formula for a problem, simulating the models can help you check that you didn’t make an error in deriving your formula - as the simulation should give you approximately the same value as that predicted by the formula you derived.