Created
November 22, 2014 07:10
-
-
Save brycethomas/bf316f6b79e1848c556f to your computer and use it in GitHub Desktop.
Monty Hall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
cars, donkeys = 0, 0 | |
trials = 10000 | |
doors = ['a', 'b', 'c'] | |
for i in range(trials): | |
# place the car behind a random door | |
car_door = random.choice(doors) | |
# choose first door at random | |
first_choice = random.choice(doors) | |
# reveal a donkey from a random door that is not the first choice | |
revealed_donkey = random.choice( | |
[d for d in doors if d != first_choice and d != car_door] | |
)[0] | |
# switch door to the only remaining door | |
second_choice = [d for d in doors \ | |
if d != first_choice and d != revealed_donkey][0] | |
if second_choice == car_door: | |
# final choice is car | |
cars += 1 | |
else: | |
# final choice is donkey | |
donkeys += 1 | |
# prints something like "cars: 6765 donkeys: 3235 car %: 0.6765 | |
print 'cars:', cars, 'donkeys:', donkeys, 'car %:', cars/float(donkeys + cars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment