Last active
August 29, 2015 14:16
-
-
Save scribu/a1d37a2b62b2b64ab960 to your computer and use it in GitHub Desktop.
Simulation for the Monty Hall problem.
This file contains hidden or 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 | |
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