Skip to content

Instantly share code, notes, and snippets.

@pycrawling
Created September 20, 2020 10:16
Show Gist options
  • Save pycrawling/5859bbad739189e0c288dc379ae64ccf to your computer and use it in GitHub Desktop.
Save pycrawling/5859bbad739189e0c288dc379ae64ccf to your computer and use it in GitHub Desktop.
Monty Hall problem
# Monty Hall problem
# https://comdoc.tistory.com/
# 2020-09-19
from random import randint
def monty_hall():
car = randint(0, 2)
first_choice = randint(0, 2)
opened_door = car
while opened_door == car or opened_door == first_choice:
opened_door = randint(0, 2)
second_choice = first_choice
while second_choice == first_choice or second_choice == opened_door:
second_choice = randint(0, 2)
# print(f"car:{car}, first_choice:{first_choice}, opened_door:{opened_door}, second_choice:{second_choice}")
return car == first_choice, car == second_choice
hits_of_first = 0
hits_of_second = 0
total = 10000
for _ in range(total):
hit_of_first, hit_of_second = monty_hall()
if hit_of_first:
hits_of_first += 1
elif hit_of_second:
hits_of_second += 1
print(f"first: {hits_of_first}/{total} = {hits_of_first / total}")
print(f"second: {hits_of_second}/{total} = {hits_of_second / total}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment