Last active
December 16, 2016 19:45
-
-
Save jackfischer/ac45aa8a5fbb15abc58b51a80c6df2f5 to your computer and use it in GitHub Desktop.
Simple monty hall problem simulation
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 | |
trials = 100 | |
stay = [] | |
switch = [] | |
for t in range(trials): | |
possibilities = [1,2,3] | |
ans = random.choice(possibilities) | |
stay_ans = random.choice(possibilities) | |
stay.append(stay_ans == ans) #either true or false | |
possibilities = set(possibilities) | |
discard = possibilities.difference(set((stay_ans, ans))).pop() | |
switch_ans = possibilities.difference(set((stay_ans, discard))).pop() | |
switch.append(switch_ans == ans) | |
stay_win = sum(stay)*100/len(stay) | |
print("Stay win %d%%" % stay_win) | |
switch_win = sum(switch)*100/len(switch) | |
print("Switch win %d%%" % switch_win) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment