The Monty Hall Problem In PHP

24th September 2013 - 12 minutes read time

The Monty Hall problem is a counter intuitive problem in probability mathematics that deals with picking the right prize from a set of three doors. The problem is named after the television celebrity Monty Hall and is loosely based on the USA game show Let's Make a Deal.

This has become a popular problem in programming as it is a good exercise in thinking through a problem to prove what outcome actually occurs. Lots of examples have been posted online so I thought I would sit down and attempt to solve it myself. The problem is most commonly summarised as follows (this example was taken from Rosetta Code):

Suppose you’re on a game show and you’re given the choice of three doors. Behind one door is a car; behind the others, goats. The car and the goats were placed randomly behind the doors before the show.

The rules of the game show are as follows:

After you have chosen a door, the door remains closed for the time being. The game show host, Monty Hall, who knows what is behind the doors, now has to open one of the two remaining doors, and the door he opens must have a goat behind it. If both remaining doors have goats behind them, he chooses one randomly. After Monty Hall opens a door with a goat, he will ask you to decide whether you want to stay with your first choice or to switch to the last remaining door.

For example:

Imagine that you chose Door 1 and the host opens Door 3, which has a goat. He then asks you "Do you want to switch to Door Number 2?" Is it to your advantage to change your choice?

Note that the player may initially choose any of the three doors (not just Door 1), that the host opens a different door revealing a goat (not necessarily Door 3), and that he gives the player a second choice between the two remaining unopened doors.

Simulate at least a thousand games using three doors for each strategy and show the results in such a way as to make it easy to compare the effects of each strategy.

Rather than just post a block of code I thought I would go through the steps I took to generate the numbers of cars won using each strategy.

The first thing I needed was a place to store the results of each game. This meant creating an array so that I could store how many goats and cars were found for the switch and stick strategies.

  1. // Set up initial game results table
  2. $results = [
  3. 'stick' => [
  4. 'goat' => 0,
  5. 'car' => 0,
  6. ],
  7. 'switch' => [
  8. 'goat' => 0,
  9. 'car' => 0,
  10. ],
  11. ];

Before creating a loop to test lots of games I decided to create a single game of each strategy. The first thing to do is to set up the game so that there are three doors, two of which contain a goat and one contains a car. A simple array was created to store this information, and the doors were then shuffled in order to create a level of chance.

  1. // setup the game
  2. $doors = array_fill(1, 3, 'goat');
  3. $doors[array_rand($doors)] = 'car';
  4. shuffle($doors);

The first thing that the contestant does in this game is to pick a door. This is done using the array_rand() function, which returns an element key from the doors array.

  1. // contestant picks a door
  2. $pick = array_rand($doors);

With the first door chosen Monty Hall then needs to open one of the other doors that isn't the car. A simple loop is used to go through the doors array and pick out the first door that is a goat and also hasn't already been picked by the contestant. We simulate this by simply removing the door from the doors array.

  1. // Monty Hall shows a door that the contestant hasn't picked and contains a goat.
  2. foreach ($doors as $id => $door) {
  3. if ($door == 'goat' && $id != $pick) {
  4. unset($doors[$id]); break;
  5. }
  6. }

Now we come to the part where the contestant either elects to stick with the door they have chosen or picks the other door, which is where the results array comes in. As the results array has the same keys as the values of the doors array we just select the door element using the contestants choice and increment the count in the results array. We can do both of these strategies during the same game, but the stick strategy must be done first. Recording the result of the stick strategy is simple.

  1. // stick with the same door
  2. $results['stick'][$doors[$pick]]++;

The switch strategy is slightly different. We know that we want to select the other element in the doors array (remember we removed one earlier on) but we can't be sure what the key of that element is. We therefore need to loop through the doors array and pick the element that has a different key to the current contestant selection. The selection is then stored in the results array.

  1. // switch to another door
  2. foreach ($doors as $id => $door) {
  3. if ($id != $pick) {
  4. $pick = $id; break;
  5. }
  6. }
  7. $results['switch'][$doors[$pick]]++;

What we have done so far is simulate a single game and recorded the result of what happens with each strategy. The next step here is to create a simple loop that runs the games 1 million times. A sufficiently large number of games is needed to increase the accuracy of the results that we get out of the end. Here is the steps detailed above contained within a loop.

  1. // Double iterations as strategies are done separately.
  2. $iterations = 1000000;
  3. for ($i = 0; $i < $iterations; ++$i) {
  4. // setup the game
  5. $doors = array_fill(1, 3, 'goat');
  6. $doors[array_rand($doors)] = 'car';
  7. shuffle($doors);
  9. // contestant picks a door
  10. $pick = array_rand($doors);
  12. // Monty Hall shows a door that the contestant hasn't picked and contains a goat.
  13. foreach ($doors as $id => $door) {
  14. if ($door == 'goat' && $id != $pick) {
  15. unset($doors[$id]);
  16. break;
  17. }
  18. }
  19. // stick with the same door
  20. $results['stick'][$doors[$pick]]++;
  21. // switch to another door
  22. foreach ($doors as $id => $door) {
  23. if ($id != $pick) {
  24. $pick = $id;
  25. break;
  26. }
  27. }
  28. $results['switch'][$doors[$pick]]++;
  29. }

After running the games a million times we can now work out how each strategy compares. This involves a simple percentage calculation for both the stick and the switch strategies.

  1. // print results
  2. $string = '';
  3. $string .= 'Stick: ' . ($results['stick']['car'] / $iterations) * 100 . '%' . PHP_EOL;
  4. $string .= 'Switch: ' . ($results['switch']['car'] / $iterations) * 100 . '%' . PHP_EOL;
  5. print $string;

This prints the following:

  1. Stick: 33.3427%
  2. Switch: 66.6573%

So we can see here that the best strategy is to switch doors, which confirms the result of the probability calculations. If you haven't already found out then the answer to the Monty Hall problem is to always switch doors as there is a 33% greater chance of getting the car.

One thing that is missing here is the need to show the results in a way that makes it very clear which strategy is the best. To do this I used the same percentage calculations that I had run before but passed them through a str_repeat() function so that the percentages for each strategy were represented by a line of stars. A nested loop was used in order to print the results of the number of goats and the number of cars found for each strategy.

  1. $string = '';
  2. foreach ($results as $strategy => $result) {
  3. $string .= PHP_EOL . $strategy . PHP_EOL;
  4. foreach ($result as $prize => $numbers) {
  5. $string .= $prize . "\t" . str_repeat('*', ($numbers / $iterations) * 100) . PHP_EOL;
  6. }
  7. }
  8. print $string;

This prints out the following:

  1. stick
  2. goat ******************************************************************
  3. car *********************************
  5. switch
  6. goat *********************************
  7. car ******************************************************************

This simple graph makes it very clear how much more successful the switching strategy is to the sticking strategy. I'm sure this code can be improved slightly by abstracting certain parts of repeating functionality, but I just wanted to keep things simple and generate the correct result. During my research into the Monty Hall problem I also found that many people were experimenting with the number of doors present in the problem, and often leaving the user with two doors to chose from in the end. The number of doors can easily be changed by altering the initial array_fill() function call and setting the second parameter to something larger but more work would be needed to remove everything but the contestants current door and the one with the car behind it. I haven't looked into how this effects the outcome of the situation, but I leave this as an exercise for the user.

Rather than piece this code together here is the code above in full. Feel free to play around with it.

  1. // Set up initial game results table
  2. $results = [
  3. 'stick' => [
  4. 'goat' => 0,
  5. 'car' => 0,
  6. ],
  7. 'switch' => [
  8. 'goat' => 0,
  9. 'car' => 0,
  10. ],
  11. ];
  13. $iterations = 1000000;
  14. for ($i = 0; $i < $iterations; ++$i) {
  15. // setup the game
  16. $doors = array_fill(1, 3, 'goat');
  17. $doors[array_rand($doors)] = 'car';
  18. shuffle($doors);
  20. // contestant picks a door
  21. $pick = array_rand($doors);
  23. // Monty Hall shows a door that the contestant hasn't picked and contains a goat.
  24. foreach ($doors as $id => $door) {
  25. if ($door == 'goat' && $id != $pick) {
  26. unset($doors[$id]);
  27. break;
  28. }
  29. }
  31. // stick with the same door
  32. $results['stick'][$doors[$pick]]++;
  34. // switch to another door
  35. foreach ($doors as $id => $door) {
  36. if ($id != $pick) {
  37. $pick = $id; break;
  38. }
  39. }
  40. $results['switch'][$doors[$pick]]++;
  41. }
  43. // print results
  44. $string = '';
  45. $string .= 'Stick: ' . ($results['stick']['car'] / $iterations) * 100 . '%' . PHP_EOL;
  46. $string .= 'Switch: ' . ($results['switch']['car'] / $iterations) * 100 . '%' . PHP_EOL;
  48. foreach ($results as $strategy => $result) {
  49. $string .= PHP_EOL . $strategy . PHP_EOL;
  50. foreach ($result as $prize => $numbers) {
  51. $string .= $prize . "\t" . str_repeat('*', ($numbers / $iterations) * 100) . PHP_EOL;
  52. }
  53. }
  54. print $string;

Add new comment

The content of this field is kept private and will not be shown publicly.