Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from random import shuffle, randint
def monty_hall(change=0, initial=0, verbose=False):
doors = ['goat', 'goat', 'car']
shuffle(doors) # randomize order of doors
choice = randint(0, 2) # also pick a random door
goats = []
for door in range(len(doors)):
if doors[door] == 'goat' and door != choice:
goats.append(door)
shuffle(goats) # reveal a random goat
reveal = goats[0]
# switch doors
new_choice = 0
while new_choice == choice or new_choice == reveal:
new_choice += 1
# print details
if verbose:
print(doors)
print('choice: ' + str(choice))
print('goat: ' + str(reveal))
print('switch: ' + str(new_choice))
# determine success
if doors[new_choice] == 'car':
change += 1
else:
initial += 1
return change, initial
if __name__ == "__main__":
success_change = 0
success_initial = 0
for i in range(1000):
success_change, success_initial = monty_hall(success_change, success_initial, verbose=False)
print(str(success_change) + ', ' + str(success_initial))
print(str(success_change / (success_change + success_initial) * 100) + "% chance of success when changing")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.