The 100 Prisoner Riddle is a game where 100 prisoners need to find the box with their number in a pregenerated room with 100 boxes. The problem or trick lies in the strategy they need to use for all prisoners to find their own numbers.
The idea of developing this program in MATLAB came from a video about this riddle by Veritasium. I wanted to simulate the game and its' propabilities using the math features of MATLAB (random permutations, plots, etc.).
To understand the process of the game and the meaning/explanation behind the solution and propabilities, please read the Explanation.md file. This program is not designed to explain the outcomes but rather calculate and display them. So if parts of the results are hard to comprehend, please read the explanations.
Simulating a game means that a new room is generated with boxes from 1 to
Simulating a game with
- he either opened half of the boxes in the room
- he found his note before
1.happened
The prisoners are programmed to use the loop strategy.
This functions takes a generated room and calculates the longest loop of numbers.
Running a benchmark is the most crucial function of this program. It will run multiple game simulations and collect data from them. The more games you simulate, the more precise the results will be. You can choose how many prisoners (TEST_SUBJECTS) you want to have and how many games (TOTAL_GAMES) you want to simulate. A benchmark will collect the following data:
- count of successful games (all prisoners were able to find their notes)
- count of how often which number has been the longest loop of numbers in a simulated game
- count of successful prisoners in each game
The benchmark can be run in FAST_MODE. This will essentially skip simulating each prisoner individually and collect the result data just from the amount of prisoners and the longest loop in each game. These two variables are enough to complete a game. Especially when trying to run a benchmark of huge amounts of games or prisoners, turning fast mode ON will cut the needed calculation time by a lot.
This is the main file you will work with when using this program. Running main() will set up a benchmark, collect its' data and display all needed results. At the top of the code, you can edit these three variables yourself:
TEST_SUBJECT- amount of prisoners participating in the benchmarkTOTAL_GAMES- amount of games to be simulatedFAST_MODE- true if benchmark should run in fast mode
Changing any other code in other files may break the program and is not recommended.
After a benchmark, this function will use the collected data to plot a column chart about what the propability of a number # being the longest loop in a game is.
- The blue column chart represents the actual test results.
- The red graph represents the function
$f(x) = \frac{1}{x}$ showing the exact illustration of the propabilities for values greater than$n/2$ . If you would run a benchmark of$\infty$ games, the blue bars would match the red graph completely. - If you run a benchmark with 100 test subjects, a third green graph will appear, showing the left segment of a Gaussian function. This is only an estimation for the propabilities of values smaller than
$n/2$ . The blue bars in this section are reminiscent of a Gaussian function, but cannot be accurately represented by one.
After a benchmark, this function will plot the propabilities for # successful prisoners during the simulated games. The blue bars represent the actual test results. The green bars are a result of a Bernoulli experiment where the prisoners chose randomly while playing. You can see that
This function doesn't have anything to do with the benchmark and simulations itself, but it shows the concept of permutations and loops pretty well. Running permutation_example(n) will plot a directed graph showing the individual loops formed by a randomly generated room with
The two .fig files in the projects folder are results of a benchmark with