Created
March 15, 2014 15:15
-
-
Save etaubman/9568833 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 | |
from time import time | |
class OpenDoorsOutOfRangeError(Exception): | |
def __init__(self): | |
pass | |
class Monty: | |
initial = False | |
changed = False | |
def __init__(self,door_count,open_count): | |
#Set up our door choices | |
doors = [d for d in range(1,door_count + 1)] | |
#Put our goat behind a door to find | |
goat = random.choice(doors) | |
#Guess randomly at the same set of doors | |
guess = random.choice(doors) | |
#Check if the initial guess was right | |
if goat == guess: self.initial = True | |
#Remove a door that isn't the right one | |
open = [random.choice([d for d in doors if d != goat]) for o in range(1,open_count + 1)] | |
#Swap door choices | |
changed = random.choice([d for d in doors if d not in open and d != guess]) | |
#Check if swapped choice is right | |
if changed == goat: self.changed = True | |
def main(total_tries = 100000,door_count = 3, open_count = 1): | |
try: | |
t = time() | |
if open_count >= door_count - 1: | |
raise OpenDoorsOutOfRangeError | |
m = [Monty(door_count,open_count) for x in range(total_tries)] | |
initial_perc = [x.initial for x in m].count(True) * 100 / float(total_tries) | |
changed_perc = [x.changed for x in m].count(True) * 100 / float(total_tries) | |
print "Total Tries:\t\t" + str(total_tries) | |
print "Initial Choice:\t" + str(initial_perc) + "%" | |
print "Swapped Choice:\t" + str(changed_perc) + "%" | |
run_time = round((time() - t) * 1000,0) | |
print "Run Time:\t\t\t" + str(run_time) + "ms" | |
except OpenDoorsOutOfRangeError as e: | |
print "Open Door count must be at least 2 less than Total Door Count." | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment