🧪 We're running a cheminformatics notebook competition!

Enter by October 4
23 / 103
100 Prisoners Puzzle

100 Prisoners Puzzle

by marimo team

An interactive simulation of the famous 100 prisoners probability puzzle.

About this notebook

This notebook simulates the 100 prisoners riddle from the Veritasium video it links. It generates a random permutation with NumPy, finds the cycles in that permutation, and draws the boxes as a force-directed graph. The graph is a custom anywidget that renders with D3. Each node is one box, arrows show where each box points, and each cycle gets its own color.

Two number inputs set the number of prisoners and the maximum number of boxes each prisoner can open. A button generates a new random arrangement. When you change an input or press the button, marimo re-runs the dependent cells, rebuilds the graph, and reports the longest cycle and whether all prisoners succeed. You can drag the nodes and zoom the graph.

A second section runs 100,000 simulations, computes the longest cycle in each, and plots the distribution of longest cycle lengths as an Altair bar chart through pandas. A closing section explains why a prisoner who starts at their own box always finds their number on that cycle.

Related notebooks