Created
March 14, 2018 10:24
-
-
Save allencch/6143c6908be053de127c5bed708688c3 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
#!/usr/bin/env python | |
# This is simulating Monty Hall Paradox | |
import random | |
def monty(switch): | |
# random shuffle | |
doors = [0, 0, 1] # one of the door contains the car | |
random.shuffle(doors) | |
openDoor = None | |
# choose the first door (not open) | |
# if the first door is 1, randomly open the other | |
if doors[0] == 1: | |
# open the door | |
openDoor = random.randint(1, 2) | |
else: # open the door that contains goat | |
if doors[1] == 1: | |
openDoor = 2 | |
else: | |
openDoor = 1 | |
# now open the last door | |
if not switch: | |
return doors[0] | |
else: | |
if openDoor == 2: | |
return doors[1] | |
else: | |
return doors[2] | |
def main(): | |
total = 10000 | |
car = 0 | |
for i in range(total): | |
car += monty(True) | |
print("Always switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total)) | |
car = 0 | |
for i in range(total): | |
car += monty(False) | |
print("No switch the door. Total: {}, car: {}. P = {}".format(total, car, car / total)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment