Posted in fun, tagged fun, programming, puzzle on June 29, 2009 |
Leave a Comment »
After yesterday’s discussion with my family at Sunday tea, I decided to write a computer simulation of the Monty Hall problem.
This is not a fancy thing with graphics etc. but a basic JAVA program that randomly (according to JAVA’s Random class) assigned a car, picked the contestant’s door, opened the appropriate door and summed, over 1,000,000,000 iterations, the win/loose chances of staying or switching. The following are the % of times that strategy won:
swap = 66.66721%
stay = 33.33278%
I realised after finishing the code that the code did not use the open variable (which represents the door the host opened), in other words it could calculate the stats without actually knowing which door the host opened after the players initial choice.
The meat of the code therefore reduced to:
int behind = random.nextInt(3); // this is where the car is
int choice = random.nextInt(3); // this is the contestant’s choice
if (choice == behind) stay++; // staying results in win
else swap++; //swapping results in a win
Looking at that code is, to me, probably the clearest explanation of the result.
Did I miss something?
Read Full Post »