import random
repetitions = 10000
doors = [1,2,3]
def experiment(repetitions, montyKnows = True):
switchWins = 0
keepWins = 0
goatReveals = 0
for i in xrange(repetitions):
# The door containing the car
carDoor = random.choice(doors)
# The door I pick
myDoor = random.choice(doors)
# Monty will open one of the two doors I didn't pick
if montyKnows:
# If Monty knows which door has the car, he randomly opens one that doesn't.
montyDoor = random.choice([d for d in doors if d != myDoor and d != carDoor])
else:
# If he doesn't, he randomly opens one of the two doors
montyDoor = random.choice([d for d in doors if d != myDoor])
# If it contains a car, we don't count this experiment and try again
if montyDoor == carDoor:
continue
# At this point, Monty has opened a door which doesn't contain a car, so the car is either behind my door, or the other closed one
goatReveals += 1
if myDoor == carDoor:
keepWins += 1
else:
switchWins += 1
if montyKnows:
print "Monty knew which door contained the car, and intentionally opened one that didn't."
else:
print "Monty randomly chose one of the two doors I didn't pick to open."
print "We ran the experiment %s times."%repetitions
print "Monty revealed a goat %s times."%goatReveals
print "Switching would have gotten us the car %s times, which is %.1f%% of the times he revealed a goat."%(switchWins, switchWins*100./goatReveals)
print "Keeping our door would have gotten us the car %s times, which is %.1f%% of the times he revealed a goat."%(keepWins, keepWins*100./goatReveals)
print "Experiment 1:"
experiment(repetitions, montyKnows = True)
print
print "Experiment 2:"
experiment(repetitions, montyKnows = False)