Skip to content

Instantly share code, notes, and snippets.

@stewartpark
Last active February 25, 2016 08: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 stewartpark/0661a484a1f57af89ec6 to your computer and use it in GitHub Desktop.
Save stewartpark/0661a484a1f57af89ec6 to your computer and use it in GitHub Desktop.
Monty Hall Problem
"""
https://en.wikipedia.org/wiki/Monty_Hall_problem
"""
import random
def do_game(change_selection):
# Initial door
doors = [1, 0, 0]
random.shuffle(doors)
# Select one
selection = random.randint(0, 2)
# Host lets the participant know
_ones = [x for x in range(3) if x != selection]
wrong_one = _ones[0] if doors[_ones[0]] == 0 else _ones[1]
left_ones = [x for x in range(3) if x != wrong_one]
# Do the condition
if change_selection:
selection = [x for x in left_ones if x != wrong_one][0]
# Check answer
if doors[selection] == 1:
return True
else:
return False
a = 0
b = 0
for _ in xrange(100000):
a += do_game(True)
b += do_game(False)
print 'When you select the other one:', a
print 'When you don\'t:', b
print 'Ratio: ', 1.0 * a / (a + b) # This will give you roughly about 0.6xx
print 'Ratio: ', 1.0 * b / (a + b) # This will give you roughly about 0.3xx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment