r/dailyprogrammer • u/oskar_s • May 07 '12
[5/7/2012] Challenge #49 [easy]
The Monty Hall Problem is a probability brain teaser that has a rather unintuitive solution.
The gist of it, taken from Wikipedia:
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? (clarification: the host will always reveal a goat)
Your task is to write a function that will compare the strategies of switching and not switching over many random position iterations. Your program should output the proportion of successful choices by each strategy. Assume that if both unpicked doors contain goats the host will open one of those doors at random with equal probability.
If you want to, you can for simplicity's sake assume that the player picks the first door every time. The only aspect of this scenario that needs to vary is what is behind each door.
- Thanks to SleepyTurtle for posting this idea at /r/dailyprogrammer_ideas! Do you have a problem you think would be good for us! Head on over there and post it!
3
u/luxgladius 0 0 May 07 '12
Perl
Made a few player interfaces, one being a human player so you can play yourself, one being a persitent player that sticks to his original choice, and one being a smart player that always switches. I do the human play once, just for fun, and then do 100,000 trials with each of the other two types of player to get the relevant statistics.
Output
Which door (0, 1, 2)? 0
The host opens door 2 revealing a goat!
Which door (0, 1)? 1
You won a goat!
STUPID! YOU'RE SO STUPID!
PersistentPlayer: car: 33361,goat: 66639
SmartPlayer: car: 66535,goat: 33465
1
u/hipsterindisguise May 07 '12
Python
First time trying to write with Python and I think it went fairly well.
1
u/ixid 0 0 May 07 '12 edited May 07 '12
Coding it and adding the assumption that the player always picks door one makes the answer seem so obvious that I can't believe I was ever confused by it, I did find it very unintuitive when first introduced to it. I started off with boxes and removals but it's just placing the car behind a door from 1 to 3 at heart, doors 2 and 3 are just one megadoor.
1
u/[deleted] May 07 '12
Python
Assuming the contestant always picks Door 1 makes this easy to simplify.
1
u/emcoffey3 0 0 May 07 '12
Here's my solution in C#. It could probably be a bit more compact, but oh well...
Ouput from the above program:
1
u/[deleted] May 07 '12
Example Output:
Switcher: 0.678 Stayer: 0.322
1
u/deuterium64 May 09 '12
Java
Long-winded, but gets there eventually.
1
u/school_throwaway May 09 '12
python (not 100% sure i did this right though, just tried to represent probabilities)
1
u/loonybean 0 0 May 14 '12
Javascript:
The higher the number of tries, the closer the good-switch proportion gets to exactly two-third.
1
u/TSwift13 May 27 '12
If the contestant switches doors every time the then it's about 3/5 chance of winning the car.
http://i.imgur.com/nNgHt.png
1
u/bigronaldo May 31 '12
In C#. Looks like I followed the same logic as huck_cussler. The only way a switcher can win is if they select the wrong door initially. And vice versa for a non-switcher. Output based on 1,000,000 iterations.
Output:
1
u/Should_I_say_this Jul 05 '12 edited Jul 05 '12
Wow, everyone wrote a much simpler answer than mine...meh. Heres mine anyways:
plays the game X number of times always switching
plays the game X number of times never switching
This plays X games simultaneously with 1 person always switching and 1 never switching. It then prints the results to compare
1
u/huck_cussler 0 0 May 07 '12
So, I worked out the logic to simplify it a little bit. Basically, if the contestant doesn't pick the right door the first time but will switch, then it is a guaranteed win. It's also the only way a 'switcher' will win. On the other hand, the only way a non-switcher will win is if he picks the right door the first time.
I've run it over 100 iterations both ways several times and it usually comes out right around 0.67 for the switcher and 0.33 for the non.
0
u/n0rs May 07 '12 edited May 07 '12
Python:
Results:
Err, missed the "simplicities's sake" part... woops.