Last active
January 12, 2016 23:17
-
-
Save jsheedy/00653f6a0b72d421d17a to your computer and use it in GitHub Desktop.
numerical solution of the monty hall problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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