Last active
February 7, 2019 15:46
-
-
Save ortsed/4ea2f81903fd243176c90cec594d2342 to your computer and use it in GitHub Desktop.
Simulate Monty Hall Problem
This file contains hidden or 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
from random import randint | |
i = 0 | |
# count of wins without switching choice, with switching | |
without_switching = 0 | |
with_switching = 0 | |
iterations = 100000 | |
while i <iterations: | |
#set the list of doors with a prize behind a random one | |
prize = randint(0,2) | |
# make a random first choice | |
first = randint(0,2) | |
# open a door that's not the same as the first choice | |
choices = [0,1,2] | |
choices.remove(first) | |
open = choices[randint(0,1)] | |
# if the first opened door has the prize, both strategies are not successful | |
if open == prize: | |
pass | |
# if the opened door did not have the prize | |
elif open != prize: | |
# define the second choice as the door that wasn't open and wasn't first choice | |
choices = [0,1,2] | |
choices.remove(first) | |
choices.remove(open) | |
second = choices[0] | |
if second == prize: | |
with_switching = with_switching + 1 | |
elif first == prize: | |
without_switching = without_switching + 1 | |
i = i +1 | |
net_wins_from_switching = with_switching - without_switching | |
print("Net wins from switching: %s" % net_wins_from_switching) | |
print("Winning %% from switching: %s" % (100 * with_switching/iterations)) | |
print("Winning %% without switching: %s" % (100 * without_switching/iterations)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment