Created
May 19, 2020 13:14
-
-
Save whatalnk/4859b8c52718b3c92ba71a1195a6bc48 to your computer and use it in GitHub Desktop.
Monty Hall Problem
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 argparse | |
from random import random | |
from fractions import Fraction | |
def monty(change=True): | |
right = 1 | |
x = random() | |
if x <= Fraction(1 / 3): | |
answer = 1 | |
wrong = 2 | |
elif x <= Fraction(2 / 3): | |
answer = 2 | |
wrong = 3 | |
else: | |
answer = 3 | |
wrong = 2 | |
if change: | |
answer = 6 - answer - wrong | |
if right == answer: | |
return True | |
else: | |
return False | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description="Simulate Monty Hall Problem") | |
parser.add_argument("-n", "--num", type=int, help="number of iteration") | |
args = parser.parse_args() | |
n = 1000 | |
if args.num: | |
n = args.num | |
cnt = sum([1 for i in range(n) if monty(change=True)]) | |
print("Change: ", cnt / n) | |
cnt = sum([1 for i in range(n) if monty(change=False)]) | |
print("Not change: ", cnt / n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment