Skip to content

Instantly share code, notes, and snippets.

@RantyDave
Created June 24, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RantyDave/f0b5db1d237e04368b2e9c84cacf6556 to your computer and use it in GitHub Desktop.
Save RantyDave/f0b5db1d237e04368b2e9c84cacf6556 to your computer and use it in GitHub Desktop.
The Monty Hall Problem
from random import randint
def change_mind(i_chose, host_opened):
if i_chose == 1 and host_opened == 2:
return 3
if i_chose == 1 and host_opened == 3:
return 2
if i_chose == 2 and host_opened == 1:
return 3
if i_chose == 2 and host_opened == 3:
return 1
if i_chose == 3 and host_opened == 1:
return 2
if i_chose == 3 and host_opened == 2:
return 1
def one_round(changed_mind):
car_behind_door = randint(1, 3)
i_choose = randint(1, 3)
host_opens = randint(1, 3)
while host_opens == car_behind_door or host_opens == i_choose:
host_opens = randint(1, 3)
print(f"Car is behind door {car_behind_door}. I choose door {i_choose}. The host opens {host_opens}.")
if changed_mind:
i_choose = change_mind(i_choose, host_opens)
print(f"I change my mind and choose {i_choose} instead")
else:
print("I stick with my original guess")
win = i_choose == car_behind_door
print("WIN!" if win else "lose")
return win
wins = 0
for n in range(0, 1000):
if one_round(True):
wins += 1
print(f"{wins} total wins")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment