Skip to content

Instantly share code, notes, and snippets.

@brycethomas
Created November 22, 2014 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycethomas/bf316f6b79e1848c556f to your computer and use it in GitHub Desktop.
Save brycethomas/bf316f6b79e1848c556f to your computer and use it in GitHub Desktop.
Monty Hall
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