Last active
August 29, 2015 14:07
-
-
Save TheBB/a7e9ee4ad3bb4b14ad4e to your computer and use it in GitHub Desktop.
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 | |
import sys | |
from itertools import product | |
def random_monty(choices): | |
return random.choice(choices) | |
def deterministic_monty(choices): | |
return random.choice([c for c in choices if c != 0]) | |
def switch_contestant(init, reveal): | |
return init != 0 | |
def stay_contestant(init, reveal): | |
return init == 0 | |
def rand_contestant(init, reveal): | |
return random.choice([c for c in xrange(3) if c != reveal]) == 0 | |
def game(monty, contestant): | |
choices = [0,1,2] | |
init = random.choice(choices) | |
choices.remove(init) | |
reveal = monty(choices) | |
if reveal == 0: | |
return game(monty, contestant) | |
return contestant(init, reveal) | |
n = int(sys.argv[1]) | |
montys = [('Random Monty', random_monty), | |
('Deterministic Monty', deterministic_monty)] | |
contestants = [('Switching contestant', switch_contestant), | |
('Stubborn contestant', stay_contestant), | |
('Random contestant', rand_contestant)] | |
for (md, monty), (cd, cont) in product(montys, contestants): | |
k = 0 | |
for _ in xrange(n): | |
if game(monty, cont): | |
k += 1 | |
print '%s, %s: %.2f%%' % (md, cd, float(k) / n * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment