Skip to content

Instantly share code, notes, and snippets.

@jackfischer
Last active December 16, 2016 19:45
Show Gist options
  • Save jackfischer/ac45aa8a5fbb15abc58b51a80c6df2f5 to your computer and use it in GitHub Desktop.
Save jackfischer/ac45aa8a5fbb15abc58b51a80c6df2f5 to your computer and use it in GitHub Desktop.
Simple monty hall problem simulation
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