nonlinear-vibes/100-prisoners
Exact analysis and simulation of the 100 prisoners problem
100 Prisoners
Monte Carlo simulation and exact analysis of the 100 prisoners problem, generalized to
Compares the loop strategy to independent random guesses.
Problem
- Given
$n$ numbered prisoners and$n$ boxes, a random permutation of labels$1, …, n$ is placed in the boxes in a room. - Each prisoner walks into the room and may open at most
$n/2$ boxes to find their number. - They can agree on a strategy beforehand, but cannot communicate during play.
- They are released if everyone finds their number.
Strategies
-
Loop strategy: Prisoner
$i$ starts at box$i$ , opens it, reads label$j$ ,
then opens box$j$ , and so on (up to$n/2$ steps). If there are no cycles longer than$n/2$ , all of them will find their number. -
Random baseline: Each prisoner opens
$n/2$ random distinct boxes.
Exact analysis
Since the probability of having a longest loop shorter than
Then, we just have to add all the
For
To have a lower estimate of the probability of success, we can integrate
Which is independent of the number of prisoners
Quick start
Run prisoners_demo, it calls prisoners_sim which handles the simulations.
