Skip to content

Instantly share code, notes, and snippets.

@actsasgeek
Created August 29, 2013 02:09
Show Gist options
  • Select an option

  • Save actsasgeek/6373559 to your computer and use it in GitHub Desktop.

Select an option

Save actsasgeek/6373559 to your computer and use it in GitHub Desktop.
Monty Hall problem in Python.
from random import randint
def evaluate_a_monty_hall_scenario(switch=False):
options = set([1, 2, 3])
car = randint( 1, 3)
pick = randint( 1, 3)
opened = list( options.difference( set([car])).difference( set([pick])))[0]
closed = list( options.difference( set([pick])).difference( set([opened])))[0]
if switch:
pick = closed
return car == pick
def evaluate_monty_hall_problem( switch=False):
trials = 10000
count = 0
for i in range( 0, trials):
result = evaluate_a_monty_hall_scenario( switch)
if result:
count += 1
return float( count) / trials
evaluate_monty_hall_problem(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment