Skip to content

Instantly share code, notes, and snippets.

@JFlynnXYZ
Created May 3, 2018 08:49
Show Gist options
  • Save JFlynnXYZ/26e84e16364fac123a6d73f3a04f4138 to your computer and use it in GitHub Desktop.
Save JFlynnXYZ/26e84e16364fac123a6d73f3a04f4138 to your computer and use it in GitHub Desktop.
import random as r
from argparse import ArgumentParser
import sys
def main(args):
switch = not args.no_switch
successful_hits = 0
for i in range(args.tries):
correct_door = r.randint(0, 2)
doors = [True if x == correct_door else False for x in xrange(3)]
choice = r.randint(0, 2)
remove_door = r.choice([j for j, x in enumerate(doors) if not x and j != choice])
final_choice = next(x for x in xrange(3) if x not in (choice, remove_door)) if switch else choice
successful_hits += final_choice == correct_door
print "Out of {:,} tries, if you {}switch you are successful {:,} times with a percentage of {:.02f}%".format(args.tries, "don't " if not switch else "",
successful_hits, float(successful_hits)/args.tries * 100 )
return 0
if __name__ == "__main__":
ap = ArgumentParser(description="The monty hall problem solved over a set number of tries")
ap.add_argument("--tries", "-t", help="The number of tries to attempt", default=10000, type=int)
ap.add_argument("--no-switch", "-sn", help="Whether to stop switching", action="store_true")
args = ap.parse_args()
sys.exit(main(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment