Skip to content

Instantly share code, notes, and snippets.

@jsheedy
Last active January 12, 2016 23:17
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 jsheedy/00653f6a0b72d421d17a to your computer and use it in GitHub Desktop.
Save jsheedy/00653f6a0b72d421d17a to your computer and use it in GitHub Desktop.
numerical solution of the monty hall problem
import random
GOAT = 0
CAR = 1
TRIALS = 10**4
def run_trial(switch=False):
choice = random.randint(0,2)
doors = [GOAT, GOAT, CAR]
random.shuffle(doors)
if switch:
return not doors[choice] and CAR or GOAT
else:
return doors[choice]
def run_trials(switch=False):
wins = sum(run_trial(switch) for _ in range(TRIALS))
return wins / TRIALS
print("Probability of winning when not switching: {:.3f}".format(run_trials(switch=False)))
print("Probability of winning when switching: {:.3f}".format(run_trials(switch=True)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment