Skip to content

Instantly share code, notes, and snippets.

@atomrigs
Last active July 20, 2019 21:39
Show Gist options
  • Select an option

  • Save atomrigs/3e7733102382d0e4c9032c34e5ab8cbb to your computer and use it in GitHub Desktop.

Select an option

Save atomrigs/3e7733102382d0e4c9032c34e5ab8cbb to your computer and use it in GitHub Desktop.
Monty Hall Problem Simulation
def calc_monty_hall(cnt):
""" Monty Hall Problem Simulation
https://en.wikipedia.org/wiki/Monty_Hall_problem
"""
import random
doors = set([1,2,3])
cnt_player_noswitch = 0
cnt_player_switch = 0
cnt_player_random = 0
for i in range(1, cnt+1):
cadillac = random.choice(list(doors))
player = random.choice(list(doors))
host = random.choice(list(doors - set([cadillac, player])))
print("Round %s --> cadillac: %s, player: %s, host: %s" % (i, cadillac, player, host))
player_switch = (doors - set([player, host])).pop()
player_random = random.choice(list(doors-set([host])))
print("Round %s switch --> cadillac: %s, player: %s, player_switch: %s, player_random: %s" \
% (i, cadillac, player, player_switch, player_random))
if cadillac == player:
cnt_player_noswitch += 1
if cadillac == player_switch:
cnt_player_switch += 1
if cadillac == player_random:
cnt_player_random += 1
print("Winning Ratio ==> No_switch: %.2f%%, Switch: %.2f%%, Switch_random: %.2f%% "\
% (100*cnt_player_noswitch/cnt, 100*cnt_player_switch/cnt, 100*cnt_player_random/cnt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment