Last active
September 26, 2017 06:00
-
-
Save bendavis78/8482a0973f31696c54bc913efcc6abfe to your computer and use it in GitHub Desktop.
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
import sys | |
import random | |
results = [] | |
strategy = 'stay' | |
if len(sys.argv) > 1: | |
strategy = sys.argv[1] | |
for i in range(0, 100): | |
# Shuffle doors | |
doors = ['car', 'goat', 'goat'] | |
random.shuffle(doors) | |
print('Doors: {}'.format(', '.join(doors))) | |
# Monty knows the goats | |
goats = [i for i, door in enumerate(doors) if door == 'goat'] | |
# Player chooses | |
choice = random.randrange(0, 3) | |
print('Player chooses door {}'.format(choice + 1)) | |
# Monty reveals one of the goats | |
revealed = random.choice(list(set(goats) - set([choice]))) | |
print('Monty reveals goat behind door {}'.format(revealed + 1)) | |
if strategy == 'switch': | |
choice = list(set([0, 1, 2]) - set([revealed]) - set([choice]))[0] | |
print('Player chooses door {}, a {}.'.format((choice + 1), doors[choice])) | |
results.append(doors[choice]) | |
print() | |
num_cars = len([r for r in results if r == 'car']) | |
print('Success rate: {}%'.format(int(num_cars / len(results) * 100))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment