C++ Simulator for the Monthy Hall Game

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. You pick a door, say No. 1 [but the door is not opened], and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?

[From Wikipedia]

Introduction

The game is simple: first the player chooses a door, than the host opens one of the remaining doors which has a goat behind it, then the player is offered the chance to change door. At the end, the last door the player chose is opened and the hidden object is the player’s prize. The player’s objective is to open the door that hides the car.

To understand the gist of the game, whether it should be better to change the door or to keep the previously selected one, consider this simple explanation.

If you want to change door, then you will win if your first choice was a goat-door.
If you don’t want to change door, then you will win if your first choice was the car-door.

At the beginning, you can pick a goat-door with a probability of 2/3 (66%).
At the beginning, you can pick a car-door with a probability of 1/3 (33%).

If you decide to change door, you will win with a probability of 2/3.
If you decide not to change door, you will win with a probability of 1/3.

Well, this is the game, then it comes the simulator.

The Simulator

Code here: https://github.com/vincepii/MontyHallSimulator

I was watching the football match between Portugal and Czech Republic and getting bored.
My brother proposed me to implement a model to simulate the Monty Hall problem, and that was a great idea.

While I was at coding, I decided that it was more interesting to develop a full emulator of the game, rather than a tiny mathematical simulation model, and this is what I did. Nevertheless, the emulator performs well also for simulations (10,000,000 runs of the game in less than 7 seconds on my notebook).

If you want to try it, or take a look at the code, you can find it on GitHub.

If you are only interested in some results, this is what happens when running 10,000,000 cycles:

vince@vince-VPCSB1C5E:~$ montyhallsimulator --cycles 10000000

Number of games (total): 10000000
Number of times the player changed doors: 5002849
Games result (WON/TOTAL): 5003182/10000000 (50.0318%)
Winning rate when changing door: 3336568/5002849 (66.6934%)
Winning rate when NOT changing door: 1666614/4997151 (33.3513%)

It is funnier to see what happens every game, using the verbose mode:

vince@vince-VPCSB1C5E:~$ montyhallsimulator --cycles 1 --verbose

The player Chose door number 2
The host Chose door number 1
The Player Changed its door and picked door number 0
The Player Won the game, car behind door number 0

The simulator also works with more than three doors, applying the same rules. This could be interesting for statistical analysis.

For the complete list of options, just type – -help:

vince@vince-VPCSB1C5E:~$ montyhallsimulator --help
Program options:
  --help                Help message
  --doors arg (=3)      Number of doors (default: 3)
  --cycles arg (=1)     Number of runs of the game (default: 1)
  --verbose             Verbose mode, prints player and host actions (default: 
                        false)

Technicalities

The Pseudo-Random-Number-Generator that the simulator uses is the boost::mt19937, initialized with a time-dependent seed.

In order to test different PRNGs easily, the generator has been encapsulated inside an Adapter class and instantiated with a Singleton to ensure that always the same generator with the same seed is used across a simulation.

I have used the boost::program_options library for options parsing. If downloading the run-time libraries (e.g.: libboost-program-options-dev package under Ubuntu) is a problem for you, the main.cpp file can be easily edited to manually assign values to the doors, cycles, and verbose variables. Then the code for options handling can be removed.

I’ve used the CMake build system to create Makefiles for the code. That’s another requirement.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s