Skip to content

Instantly share code, notes, and snippets.

@bendavis78
Last active September 26, 2017 06:00
Show Gist options
  • Save bendavis78/8482a0973f31696c54bc913efcc6abfe to your computer and use it in GitHub Desktop.
Save bendavis78/8482a0973f31696c54bc913efcc6abfe to your computer and use it in GitHub Desktop.
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