Created
July 29, 2016 03:48
-
-
Save takuti/f6e93c6e8e6128026124ed0b40a77d4b 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 | |
class MontyHall: | |
def __init__(self): | |
pass | |
def is_won(self, action='stay'): | |
doors = [1, 2, 3] | |
hit = random.choice(doors) | |
choice = random.choice(doors) | |
if action == 'stay': | |
return choice == hit | |
# delete one unchosen "miss" door | |
delete = random.choice(list(set(doors) - set([choice, hit]))) | |
# move: check if the unchosen remaining door is "hit"? | |
return list(set(doors) - set([choice, delete]))[0] == hit | |
def simulate(self, n=10000): | |
n_win_stay = sum([self.is_won('stay') for i in range(n)]) | |
p_win_stay = n_win_stay / n | |
return p_win_stay, 1 - p_win_stay | |
if __name__ == '__main__': | |
p_win_stay, p_win_move = MontyHall().simulate(10000) | |
print(p_win_stay, p_win_move) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment