The Monty Hall problem simulated
On being reminded of the Monty Hall problem on Giant Bomb’s Beastcast just a few days ago, I looked into it further and decided to try to simulate it as best I could, with only my very rudimentary coding skills in C++.
The problem itself imagines a game show scenario, where you have to pick one of three doors in the hope of winning a car that is hidden behind one of them. Behind the other two doors are donkeys. You make your pick, and then the presenter opens one of the other doors, revealing a donkey, and asks whether you’d like to switch to the other unopened door. The question is whether you should switch. The answer is that you probably should, as you have a 2:3 chance of getting the car by doing so, and only a 1:3 chance if you stick. There are good explanations for this unintuitive fact in the Wikipedia article linked above.
I’ve simulated up to 1,000,000 rounds, with two contestants, “Phil” and “Jeff”. Phil always sticks, while Jeff always switches. The results are as the theory predicts. Phil wins about 1/3 of the time, and Jeff about 2/3s of the time. Here is the output of my program from a 100 turn round. You see at the end that Phil gets just 36 wins to Jeff’s 63.
Starting... In round 1, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 2, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 3, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 4, Jeff picked 1 and switched to 3, and the winning door was 1, so he lost. Meanwhile, Phil picked 1, so he won. In round 5, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 6, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 7, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 8, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 2, so he lost. In round 9, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 10, Jeff picked 1 and switched to 3, and the winning door was 1, so he lost. Meanwhile, Phil picked 3, so he lost. In round 11, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 12, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 1, so he lost. In round 13, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 14, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 15, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 2, so he won. In round 16, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 1, so he lost. In round 17, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 2, so he lost. In round 18, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 2, so he lost. In round 19, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 20, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 21, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 22, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 1, so he lost. In round 23, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 24, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 2, so he lost. In round 25, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 26, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 3, so he won. In round 27, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 28, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 2, so he won. In round 29, Jeff picked 2 and switched to 1, and the winning door was 2, so he lost. Meanwhile, Phil picked 2, so he won. In round 30, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 31, Jeff picked 1 and switched to 2, and the winning door was 1, so he lost. Meanwhile, Phil picked 2, so he lost. In round 32, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 33, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 2, so he won. In round 34, Jeff picked 2 and switched to 1, and the winning door was 2, so he lost. Meanwhile, Phil picked 1, so he lost. In round 35, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 36, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 37, Jeff picked 1 and switched to 3, and the winning door was 1, so he lost. Meanwhile, Phil picked 2, so he lost. In round 38, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 39, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 3, so he lost. In round 40, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 41, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 42, Jeff picked 2 and switched to 1, and the winning door was 2, so he lost. Meanwhile, Phil picked 1, so he lost. In round 43, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 44, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 2, so he lost. In round 45, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 46, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 3, so he won. In round 47, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 48, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 2, so he won. In round 49, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 3, so he won. In round 50, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 51, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 52, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 3, so he lost. In round 53, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 54, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 1, so he lost. In round 55, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 56, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 57, Jeff picked 1 and switched to 2, and the winning door was 1, so he lost. Meanwhile, Phil picked 2, so he lost. In round 58, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 59, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 60, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 61, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 3, so he won. In round 62, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 3, so he won. In round 63, Jeff picked 2 and switched to 1, and the winning door was 2, so he lost. Meanwhile, Phil picked 2, so he won. In round 64, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 1, so he lost. In round 65, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 66, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 3, so he lost. In round 67, Jeff picked 1 and switched to 2, and the winning door was 1, so he lost. Meanwhile, Phil picked 1, so he won. In round 68, Jeff picked 1 and switched to 2, and the winning door was 1, so he lost. Meanwhile, Phil picked 1, so he won. In round 69, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 70, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 2, so he lost. In round 71, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 2, so he won. In round 72, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 2, so he lost. In round 73, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 74, Jeff picked 1 and switched to 2, and the winning door was 1, so he lost. Meanwhile, Phil picked 1, so he won. In round 75, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 76, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 77, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 78, Jeff picked 2 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 1, so he lost. In round 79, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 80, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 81, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 82, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 2, so he lost. In round 83, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 1, so he lost. In round 84, Jeff picked 1 and switched to 3, and the winning door was 3, so he won. Meanwhile, Phil picked 1, so he lost. In round 85, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 1, so he lost. In round 86, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 2, so he lost. In round 87, Jeff picked 3 and switched to 1, and the winning door was 3, so he lost. Meanwhile, Phil picked 3, so he won. In round 88, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 3, so he lost. In round 89, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 2, so he won. In round 90, Jeff picked 2 and switched to 3, and the winning door was 2, so he lost. Meanwhile, Phil picked 1, so he lost. In round 91, Jeff picked 3 and switched to 2, and the winning door was 3, so he lost. Meanwhile, Phil picked 1, so he lost. In round 92, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 2, so he won. In round 93, Jeff picked 3 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 94, Jeff picked 3 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 1, so he lost. In round 95, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 96, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 2, so he lost. In round 97, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 3, so he lost. In round 98, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. In round 99, Jeff picked 1 and switched to 2, and the winning door was 2, so he won. Meanwhile, Phil picked 2, so he won. In round 100, Jeff picked 2 and switched to 1, and the winning door was 1, so he won. Meanwhile, Phil picked 1, so he won. Phil-the-sticker won 36 times. Jeff-the-switcher won 63 times. Program ended with exit code: 0
Finally, here is the code that produced the output:
// // main.cpp // Montyhall // // Created by Ian Underwood on 12/08/2017. // Copyright © 2017 Ian Underwood. All rights reserved. //
#include <cstdlib> #include <ctime> #include <iostream>
using namespace std; int main() { srand((unsigned)time(0)); // Randomize timer
// Define integers int door_one = 0; // whatever is behind Door 1 int door_two = 0; // whatever is behind Door 2 int door_three = 0; // whatever is behind Door 3 int phil_pick = 0; // whatever door Phil picks int jeff_pick = 0; // whatever door Jeff picks to begin with int phil_wins = 0; // number of times Phil has won int jeff_wins = 0; // number of times Jeff has won int jeff_switch = 0; // whatever door Jeff switches to int winning_door = 0; // whatever is the winning door int random_door = 0; // Monty's randomly chosen goat-concealing door (if applicable) int open_door = 0; // whatever door is opened by Monty
// Inform that the programming is running std::cout << "Starting..." << std::endl;
for (int round = 1; round <= 100/* <--change to desired number of rounds*/; round++) { // reset all integers door_one = 0; door_two = 0; door_three = 0; phil_pick = 0; jeff_pick = 0; jeff_switch = 0; random_door = 0; open_door = 0; winning_door = 0;
// assign different random values (1-3) to each door // 1 = goat_a; 2 = goat_b; 3 = car door_one = (rand()%3)+1; // randomly assign an object to Door 1 while (door_two == 0 || door_two == door_one) { // so long as Door 2 is unassigned or equal to Door 1... door_two = (rand()%3)+1; // assign Door 2 a random object } while (door_three == 0 || door_three == door_two || door_three == door_one) { // so long as Door 3 is unassigned or equal to Door 2 or Door 3 door_three = (rand()%3)+1; // assign Door 3 a random object }
// check which Door (1-3) is the winning door (has the car) if (door_one == 3) { winning_door = 1; }
if (door_two == 3) { winning_door = 2; } if (door_three == 3) { winning_door = 3; }
if (winning_door == 0) { // if there is no winning door, something went wrong return 1; }
phil_pick = (rand()%3)+1; // Phil picks a door randomly jeff_pick = (rand()%3)+1; // Jeff picks a door randomly
if (jeff_pick == winning_door) { // If jeff picked the car to begin with... random_door = (rand()%3)+1; // ...Monty picks a door randomly. while (random_door == winning_door) { random_door = (rand()%3)+1; // Monty picks again if he randomly picked the winning door to open } open_door = random_door; // Monty opens this door }
else { // Jeff has picked a goat, so Monty must open the door in front of the other goat if (jeff_pick == 1 && door_two == 3) { open_door = 3; } if (jeff_pick == 1 && door_three == 3) { open_door = 2; }
if (jeff_pick == 2 && door_one == 3) { open_door = 3; }
if (jeff_pick == 2 && door_three == 3) { open_door = 1; }
if (jeff_pick == 3 && door_one == 3) { open_door = 2; }
if (jeff_pick == 3 && door_two == 3) { open_door = 1; }
if (open_door == 0) { // if there is no open door, something went wrong return 1; } }
// jeff switches to the door that isn't open or his original pick while (jeff_switch == 0 || jeff_switch == jeff_pick || jeff_switch == open_door) { jeff_switch = (rand()%3)+1; }
if (phil_pick == winning_door) { // Add to Phil's wins if he won this round phil_wins++; }
if (jeff_switch == winning_door) { // Add to Jeff's wins if he won this round jeff_wins++; }
// Output the result for this round std::cout << "In round " << round << ", Jeff picked " << jeff_pick << " and switched to " << jeff_switch << ", and the winning door was " << winning_door; if (jeff_switch == winning_door) { std::cout << ", so he won." << std::endl; } else { std::cout << ", so he lost." << std::endl; }
std::cout << "\tMeanwhile, Phil picked " << phil_pick;
if (phil_pick == winning_door) { std::cout << ", so he won." << std::endl;} else {std::cout << ", so he lost." << std::endl; } } // Output the final results std::cout << "Phil-the-sticker won "<< phil_wins << " times." << std::endl; std::cout << "Jeff-the-switcher won "<< jeff_wins << " times." << std::endl; return 0; }






