Monty Hall

The problem with learning to program is that you need something to create, something to do. It’s all very well if you’ve got a job where you can have certain tasks given to you, but when you’re trying to learn with nothing to do, it’s tricky.

In my opinion there’s three different things to do in this situation:

1. Imagine a scenario and work to that (e.g. I own a bookshop and want to create something to hold my stock).
2. Make a game! Games are fun! Unfortuantely games can also be very complicated and involved, unless you want something like hangman.
3. Take a real-world problem or question and model that.

Recently I’ve been doing the third option. There’s one problem I’ve never been able to get my head around, and I could never believe that it worked, so I decided to model it in code and see what happens. So here we go, the ‘Monty Hall’ problem. The Monty Hall scenario is one you may have heard before, but if you haven’t here it is.

“Imagine you’re a contestant on a game show. There are three doors to choose from; behind two of them are goats, behind the third is a car. Which door the car is behind is chosen at random, and only the game show host knows which one it is.

You pick a door, any door you like. The host opens one of the remaining doors to show you a goat (i.e. a ‘lose’ door), and gives you an opportunity to change your mind and pick the remaining door. The question is ‘Should you switch from your original choice to the other unopened door or not?'”

At first glance it seems like there’s no real reason to switch, but the answer I’d always heard is ‘yes, you should always switch.’ Statistically there’s a higher probability of switching and winning the car than if you stayed with your original choice, apparently. However, I wanted to see it for myself, so I wrote a program to simulate it.

The program creates three doors, puts a car behind one of them and goats behind the others. The contestant picks one at random, the host shows a losing door, the program automatically switches to the final door, and then prints out the outcome. In order to further practise, I’ve written it in different languages to see for myself the differences between them. Here’s my program in PHP:

<?php

// Monty Hall 3 doors problem simulation

function revealDoor($guess, $doors) {
for ($x = 0; $x <= 2; $x++) {
 if ($x == $guess) { //can't open the door the contestant chose...
 continue;
 }
 elseif ($doors[$x] == "Car") { //or the one with the car behind it.
 continue;
 }
 else {
 return $x;
 }
 }
}

$doors = array();
$cars = 0; //win counter
$goats = 0; //lose counter
for ($i = 0; $i < 100; $i++) { //main loop
 for ($n = 0; $n <= 2; $n++) { //fill all the doors with goats...
 $doors[$n] = "Goat";
 }
 $car = rand(0,2);
 $doors[$car] = "Car"; //...and then put the car behind one at random
 $guess = rand(0,2); //contestant's guess
 $revealedDoor = revealDoor($guess, $doors); //get a door to reveal

 for ($x = 0; $x <= 2; $x++) { //change the door
 if (($x == $revealedDoor) || ($x == $guess)) {
 continue;
 } else {
 $newDoor = $x;
 }
 }

 echo "Contestant picked door " . $guess . ", " . $revealedDoor . " was revealed. They changed to " . $newDoor . " and won a " . $doors[$newDoor] . "! | " . $doors[0] . $doors[1] . $doors[2] . "<br>";

 if ($doors[$newDoor] == "Car") {
 $cars += 1;
 } else {
 $goats += 1;
 }
}
echo "<br><br>Total cars = " . $cars . " | Total goats = " . $goats
?>

I’ve also written it in Python, but don’t have the source available to me here, so I’ll update later with that too if people want it. I know my code isn’t the prettiest, and probably not the most efficient either, but it works and I’m pleased with it.

Anyway, it turns out that yes, you should definitely switch choices in that situation. If you stick with the original choice you have a 1-in-3 chance of winning, if you switch, that changes to 2-in-3.

I’ll be posting more snippets of code as the weeks go by I think, it’s quite nice to see it ‘out there’.

Leave a Reply