Skip to content

Instantly share code, notes, and snippets.

@bcbwilla
Last active December 19, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcbwilla/5929336 to your computer and use it in GitHub Desktop.
Save bcbwilla/5929336 to your computer and use it in GitHub Desktop.
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