Create a gist now

Instantly share code, notes, and snippets.

@bcbwilla /monty.py
Last active Dec 19, 2015

What would you like to do?
Simple simulation of Monty Hall problem.
import random
#number of games to play
n = 5
#number of correct guesses on first guess
correct = 0
# run simulations
for i in range(n):
print 'cycle %i' % i
# place car behind random door
doors = ['car','goat','goat']
random.shuffle(doors)
print 'doors: %s' % doors
# contestant guesses
guess_door = random.randint(0,2)
guess = doors.pop(guess_door)
print 'first guess: %i (%s)' % (guess_door,guess)
print 'remaining doors after guess: %s' % doors
# Monty Hall reveals one of the doors that he knows contains a goat.
doors.remove('goat')
print 'remaining door after revealing goat: %s' % doors
# last unopened door
remaining_door = doors[0]
# check if you are correct on your first guess
if guess == 'car':
correct += 1
print 'Correct!\n'
else:
print 'Wrong!\n'
# correct percentage from staying on original door
per = str(100.0*correct/n)
print 'You are correct %s%% of the time when sticking with your first guess\n' % per
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment