After the interesting Mythbusters episode on the problem, I wrote a bit of Python code to simulate the results for fun. The code can be simplified considerably by breaking it into two trials, one where you always stay with your choice, and the other where you always swap. A further simplification can be had with the following two observations:
- If you always keep your choice, you only win when you select the car (guess == car)
- If you always swap, you only win when you do not select the car (guess != car)
This allows one to avoid computing the positions of the goats and the door Monty decides to remove. I am not really familiar with MATLAB, so I will just include the Python code below, but I hope it gives the basic idea for porting to your language of choice.
import random
import sys
def main():
if len(sys.argv) < 2:
print "usage: monty.py trials"
sys.exit(1)
t = int(sys.argv[1])
# Stick with door
stick_winners = 0
for i in xrange(t):
car = random.randint(1, 3)
choice = random.randint(1, 3)
if car == choice:
stick_winners += 1
print "(stick) won:", stick_winners, "out of", t, float(stick_winners) / float(t)
# Switch door
switch_winners = 0
for i in xrange(t):
car = random.randint(1, 3)
choice = random.randint(1, 3)
# If car != choice, open a goat door and switch, win the car
if car != choice:
switch_winners += 1
print "(switch) won:", switch_winners, "out of", t, float(switch_winners) / float(t)
if __name__ == "__main__":
main()