Published
In this lesson
Wide tables, equations, and code scroll sideways. Swipe, or Tab to focus them and use the left and right arrow keys.
100 prisoners and boxes
You will learn: Follow permutation cycles and explain the dependence behind collective success.
Start with: Derangements · Joint and marginal distributions
One hundred prisoners face one hundred labeled boxes. A uniformly random permutation places each prisoner’s number in exactly one box. Each prisoner enters alone and may open at most fifty boxes. The group succeeds only if everyone finds their own number. They can agree on a strategy beforehand but cannot communicate, mark boxes, or rearrange contents during the attempts.
Opening an independently chosen random half of the boxes gives each prisoner success probability one half. The chance that all those independent choices work is 2⁻¹⁰⁰≈7.89×10⁻³¹. The cycle strategy keeps the individual success probability at one half while dramatically changing the dependence between successes. Review joint events and independence and permutations.
Each prisoner may open 50 boxes. One uniformly shuffled permutation is shared by every prisoner. Changing its size or seed restarts the selected prisoner's revealed route; the boxes are never rearranged during a route. If the selected label exceeds a new smaller N, selection moves to N.
Start with box 1.
Inspect all cycles (reveals the solution)
Filled blue cycle lengths fit the budget. Outlined red ones exceed it. All prisoners succeed in this permutation exactly when every cycle fits: all succeed.
- Cycle 1, length 25: 1 → 86 → 45 → 78 → 5 → 59 → 85 → 15 → 79 → 94 → 26 → 19 → 88 → 28 → 75 → 53 → 93 → 31 → 10 → 74 → 35 → 11 → 38 → 34 → 54 → 1.
- Cycle 2, length 47: 2 → 67 → 76 → 33 → 72 → 68 → 52 → 13 → 21 → 100 → 40 → 62 → 60 → 32 → 7 → 97 → 16 → 8 → 70 → 55 → 69 → 46 → 71 → 39 → 84 → 42 → 96 → 14 → 20 → 25 → 47 → 81 → 36 → 80 → 44 → 92 → 89 → 65 → 6 → 17 → 24 → 4 → 29 → 41 → 30 → 90 → 18 → 2.
- Cycle 3, length 21: 3 → 56 → 95 → 99 → 61 → 43 → 83 → 48 → 98 → 27 → 58 → 64 → 22 → 82 → 57 → 50 → 63 → 9 → 66 → 12 → 91 → 3.
- Cycle 4, length 7: 23 → 87 → 51 → 73 → 37 → 49 → 77 → 23.
| Box label | Number inside |
|---|---|
| 1 | 86 |
| 2 | 67 |
| 3 | 56 |
| 4 | 29 |
| 5 | 59 |
| 6 | 17 |
| 7 | 97 |
| 8 | 70 |
| 9 | 66 |
| 10 | 74 |
| 11 | 38 |
| 12 | 91 |
| 13 | 21 |
| 14 | 20 |
| 15 | 79 |
| 16 | 8 |
| 17 | 24 |
| 18 | 2 |
| 19 | 88 |
| 20 | 25 |
| 21 | 100 |
| 22 | 82 |
| 23 | 87 |
| 24 | 4 |
| 25 | 47 |
| 26 | 19 |
| 27 | 58 |
| 28 | 75 |
| 29 | 41 |
| 30 | 90 |
| 31 | 10 |
| 32 | 7 |
| 33 | 72 |
| 34 | 54 |
| 35 | 11 |
| 36 | 80 |
| 37 | 49 |
| 38 | 34 |
| 39 | 84 |
| 40 | 62 |
| 41 | 30 |
| 42 | 96 |
| 43 | 83 |
| 44 | 92 |
| 45 | 78 |
| 46 | 71 |
| 47 | 81 |
| 48 | 98 |
| 49 | 77 |
| 50 | 63 |
| 51 | 73 |
| 52 | 13 |
| 53 | 93 |
| 54 | 1 |
| 55 | 69 |
| 56 | 95 |
| 57 | 50 |
| 58 | 64 |
| 59 | 85 |
| 60 | 32 |
| 61 | 43 |
| 62 | 60 |
| 63 | 9 |
| 64 | 22 |
| 65 | 6 |
| 66 | 12 |
| 67 | 76 |
| 68 | 52 |
| 69 | 46 |
| 70 | 55 |
| 71 | 39 |
| 72 | 68 |
| 73 | 37 |
| 74 | 35 |
| 75 | 53 |
| 76 | 33 |
| 77 | 23 |
| 78 | 5 |
| 79 | 94 |
| 80 | 44 |
| 81 | 36 |
| 82 | 57 |
| 83 | 48 |
| 84 | 42 |
| 85 | 15 |
| 86 | 45 |
| 87 | 51 |
| 88 | 28 |
| 89 | 65 |
| 90 | 18 |
| 91 | 3 |
| 92 | 89 |
| 93 | 31 |
| 94 | 26 |
| 95 | 99 |
| 96 | 14 |
| 97 | 16 |
| 98 | 27 |
| 99 | 61 |
| 100 | 40 |
| Probability over uniform permutations / choices | Value |
|---|---|
| One prisoner's cycle strategy succeeds | 0.5 |
| Every prisoner succeeds with cycle strategy | 0.3118278 |
| Every prisoner succeeds with independent random half-box choices | 7.888609e-31 |
Let the numbers tell you where to go
Prisoner i begins with box i. If it contains j, the next box is j; continue until finding i or exhausting the budget. A permutation splits into disjoint cycles, so this route follows the unique cycle containing i. The prisoner finds their own number when that cycle closes.
For six boxes containing [3,1,2,5,4,6], the cycles are 1→3→2→1, 4→5→4, and 6→6. Every cycle has length at most three, so all six prisoners succeed with three openings each. Prisoner 1 opens boxes 1,3,2 and finds slips 3,2,1. A cycle of length four would make every member of that cycle fail under the same budget.
The important event is therefore that the longest cycle has length at most N/2. Individual successes are highly dependent: everyone in a given cycle succeeds or fails together. Multiplying their marginal probabilities would discard exactly the dependence the strategy exploits.
Make a prediction
If one prisoner succeeds, have you proved the whole group succeeds?
Explore the answer
No. You have learned that this prisoner’s cycle fits within the budget. Another disjoint cycle can still be too long. Inspect every cycle, or equivalently the maximum cycle length, to decide the group’s outcome for the displayed permutation.
Count the bad cycles
For a specified length k, choose its k labels, arrange them into a cycle in (k−1)! ways, and permute the remaining labels in (N−k)! ways. Dividing by N! gives expected number of k-cycles:
When k>N/2, there can be at most one such cycle. Its expected count then equals its occurrence probability. Events for different lengths greater than N/2 are mutually exclusive, because two such cycles cannot fit in N labels. Hence, for even N:
For N=4 the value is 1−1/3−1/4=5/12, corresponding to ten of the twenty-four permutations. For N=100 it is approximately 0.311828, or 31.18%. As even N grows, the sum approaches ln 2, so success approaches 1−ln 2≈30.69%.
For a fixed prisoner, their cycle length is uniform on 1,…,N: count cycles containing that label to get (N−1)! permutations for each possible length. Exactly half of those lengths fit. The improved group probability comes from coordination, not an improved individual marginal chance.
Which assumptions carry the result?
The probability is over uniform permutations, not over the single permutation on screen. An adversary who knows the labels and deliberately chooses a long cycle can defeat this fixed strategy. Likewise, permitting communication, moving slips, or changing the opening budget defines another problem.
The short failure formula above uses the half-box budget. With a substantially smaller budget, several over-budget cycles can coexist, so simply adding their expected counts would overcount failure. This experiment varies even N while keeping the budget exactly N/2.
Make a prediction
Does a 31% chance mean that about 31 prisoners survive in each game?
Explore the answer
No. The event is collective: all N succeed, or the group fails. It is a probability across repeated permutations, not an expected fraction of individual successful prisoners.
Sources
Richard Stanley’s algebraic-combinatorics notes, section 12.1 present the cycle strategy and its probability calculation. The six-box walk and four-box enumeration above make the mechanism directly inspectable.