Skip to content

Instantly share code, notes, and snippets.

@alenbasic
Created April 19, 2014 08:26
Show Gist options
  • Save alenbasic/11077884 to your computer and use it in GitHub Desktop.
Save alenbasic/11077884 to your computer and use it in GitHub Desktop.
The Monty Hall Problem
import random
right = 0.0
wrong = 0.0
x = 100000
for i in range(x):
items = ['goat','prize', 'goat']
random.shuffle(items)
choice = random.randrange(0,3)
choice_name = items[choice]
for j in range(3):
if j != choice and items[j] == 'goat':
del items[j]
break
choice = items.index(choice_name)
if choice == 1:
choice = 0
else:
choice = 1
if items[choice] == 'prize':
right += 1
else:
wrong += 1
print 'You changed your choice and got the prize %.2f%% of the time.' % (right / x * 100)
print 'You changed your choice and lost the prize %.2f%% of the time.' % (wrong / x * 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment