Created
July 22, 2016 19:31
-
-
Save hikilaka/a886cfc033a4ace75d97383d2af8eade to your computer and use it in GitHub Desktop.
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
#!/bin/python3 | |
import random | |
doors = ['car', 'goat', 'goat'] | |
def monty(switch_doors): | |
random.shuffle(doors) | |
number = random.randint(0, 2) | |
remaining = [0, 1, 2] | |
remaining.remove(number) | |
random.shuffle(remaining) | |
preview = next(x for x in remaining if doors[x] == 'goat') | |
remaining.remove(preview) | |
remaining = remaining[0] | |
if switch_doors: | |
number, remaining = remaining, number | |
return doors[number] == 'car' | |
def run_test(switch, iterations): | |
win = 0 | |
loss = 0 | |
for _ in range(iterations): | |
if monty(switch): | |
win += 1 | |
else: | |
loss += 1 | |
return win, loss | |
def main(): | |
iterations = int(input('iterations? ')) | |
win, loss = run_test(False, iterations) | |
print('no switch:\twins=' + str(win) + '\tloss=' + str(loss)) | |
win, loss = run_test(True, iterations) | |
print('switch:\t\twins=' + str(win) + '\tloss=' + str(loss)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment