Created
April 6, 2022 16:38
-
-
Save chrisspen/11e3d9f3eef5f1d7b0ddf32bb9f7066f to your computer and use it in GitHub Desktop.
Simulation 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
""" | |
CKS 2022-04-06 Simulation of the Monty Hall problem. | |
""" | |
import random | |
CAR = 'c' | |
GOAT = 'g' | |
doors = [CAR, GOAT, GOAT] | |
def run_simulation(runs=100000, switch=False): | |
correct = 0 | |
total = 0. | |
for run in range(runs): | |
actual_doors = list(doors) | |
random.shuffle(actual_doors) | |
car_index = actual_doors.index(CAR) | |
our_index = random.randint(0, 2) | |
goat_index = list(set(range(3)).difference([car_index, our_index]))[0] | |
if switch: | |
our_index = list(set(range(3)).difference([our_index, goat_index]))[0] | |
correct += our_index == car_index | |
total += 1 | |
return correct/total | |
correct = run_simulation(switch=False) | |
print('Correct when !switch:', correct) | |
correct = run_simulation(switch=True) | |
print('Correct when switch:', correct) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment