Created
September 6, 2014 22:42
-
-
Save raymonstah/02dbc827a4a63f0ab3c5 to your computer and use it in GitHub Desktop.
A Monty Hall Simulation. Try to win a car!
This file contains hidden or 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
# Monty Hall Simulator | |
# Raymond Ho | |
# September 5th, 2014 | |
# Run -> python montyhall.py | |
import random | |
def greet(): | |
# Using the 'statement continuation character '\' | |
print "\nWelcome to the Monty Hall Simulator, created by Raymond Ho.\n" \ | |
+ "This program was inspired by the TV show, Numb3rs.\nYour goal is to " \ | |
+ "win a car, by picking the correct card.\nIf you lose, you go home " \ | |
+ "with a goat. No one wants a goat.\nYou will get an option to switch " \ | |
+ "your choice after a goat is reveiled.\nWill you switch or stick " \ | |
+ "with your original choice? Goodluck!\n" | |
# Generate a list of numbers given input | |
# Return the list, with the car position | |
def generateList(inputx): | |
# Initialize a list of all zeros. | |
List = [0] * inputx | |
carIndex = random.randint(0, inputx-1) | |
# Set that position to be the prize | |
List[carIndex] = 1 | |
return List, carIndex | |
def revealGoat(List, userchoice): | |
while True: | |
goat = random.randint(0, len(List) - 1) | |
if goat is not userchoice and List[goat] == 0: | |
return goat | |
# Chooses a number given a range that's not any of the indexes. | |
def getOtherChoice(rangeSize, choiceIndex, goatIndex): | |
for i in range(rangeSize): | |
if i is not choiceIndex and i is not goatIndex: | |
return i | |
# Presents user with information and gives interactive options. | |
def interactiveChoices(inputx = 3): | |
List, carIndex = generateList(inputx) | |
print "Below are %s choices. Pick one." % (inputx) | |
print "[x] " * inputx | |
for i in range(inputx): | |
print '(%s)' % i, | |
while True: | |
choice = int(raw_input('\nYour choice: ')) | |
if choice in range(inputx): | |
break | |
else: print 'Please enter one the numbers above.' | |
goatIndex = revealGoat(List, choice) | |
print 'Position (%s) contains a goat!' % goatIndex | |
print 'Given this information, would you like to change your choice?' | |
switch = raw_input('y/n: ' ) | |
if switch.lower().startswith('y'): | |
choice = getOtherChoice(inputx, choice, goatIndex) | |
print 'Your choice is now (%s)' % (choice) | |
else: | |
print 'The car is at position (%s)' % (carIndex) | |
# Chose the car | |
if choice == carIndex: | |
print 'You won the brand new car!' | |
return 0 | |
else: | |
print 'You lost. Enjoy your goat!' | |
return 1 | |
# Used to show switching raises chances of winning. | |
def runSwitchSimulator(times, inputx = 3, switch = True): | |
s = 'with' if switch else 'without' | |
print 'Running %s times, %s switching.' % (times, s) | |
win = lose = 0 | |
for _ in range(times): | |
List, carIndex = generateList(inputx) | |
# Generate random choice | |
choice = random.randint(0, inputx - 1) | |
goatIndex = revealGoat(List, choice) | |
# To switch, or not to switch.. | |
if switch: | |
choice = getOtherChoice(inputx, choice, goatIndex) | |
# Test | |
if choice == carIndex: win += 1 | |
else : lose += 1 | |
print 'Wins: %s | Losses: %s' % (win, lose) | |
print 'Win Percentage: %i' % (win / float(times) * 100) | |
# Times to run the interactive simulation. | |
def main(times = 1): | |
greet() | |
win = lose = 0 | |
for _ in range(times): | |
if interactiveChoices() == 0: | |
win += 1 | |
else: lose += 1 | |
print 'Wins: %s | Losses: %s' % (win, lose) | |
print 'Win Percentage: %i' % (win / float(times) * 100) | |
if __name__ == '__main__': | |
# Interactive | |
main() | |
# Simulators, un/comment to run. | |
runSwitchSimulator(100000, 3, True) | |
runSwitchSimulator(100000, 3, False) | |
print 'Your chances of winning are doubled if you switch. Why?' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment