Created
December 15, 2015 12:25
-
-
Save tspycher/d789cc9527d9fa5c0819 to your computer and use it in GitHub Desktop.
The Monty Hall Problem in Python
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 | |
summary = {"lose_inital":0, "win_initial":0, "lose":0, "win":0} | |
def game(switch=False): | |
fields = [False,False,False] | |
winField = random.randint(0,2) | |
fields[winField] = True | |
setField = random.randint(0,2) | |
if winField == setField: | |
summary['win_initial'] += 1 | |
else: | |
summary['lose_inital'] += 1 | |
removeField = winField | |
while winField == removeField or setField == removeField: | |
removeField = random.randint(0,2) | |
fields[removeField] = None | |
if switch: | |
for i in range(0,3): | |
if fields[i] == None or i == setField: | |
continue | |
setField = i | |
break | |
if winField == setField: | |
summary['win'] += 1 | |
else: | |
summary['lose'] += 1 | |
for i in range(0,1000000): | |
game(True) | |
print summary | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment