Skip to content

Instantly share code, notes, and snippets.

@scribu
Last active August 29, 2015 14:16
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 scribu/a1d37a2b62b2b64ab960 to your computer and use it in GitHub Desktop.
Save scribu/a1d37a2b62b2b64ab960 to your computer and use it in GitHub Desktop.
Simulation for the Monty Hall problem.
import random
DOORS = set([1, 2, 3])
def play(picked_door, do_switch):
door_with_car = random.sample(DOORS, 1)[0]
revealed_door = random.sample(DOORS.difference([door_with_car]), 1)[0]
if do_switch:
remaining_door = list(DOORS.difference([picked_door, revealed_door]))[0]
return remaining_door == door_with_car
return picked_door == door_with_car
def play_several(do_switch):
wins = 0
for x in range(0, 10000):
picked_door = random.sample(DOORS, 1)[0]
if play(picked_door, do_switch):
wins += 1
return wins
print("No switch: ", play_several(False))
print("With switch: ", play_several(True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment