Skip to content

Instantly share code, notes, and snippets.

@scribu
Last active Aug 29, 2015
Embed
What would you like to do?
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