[ create a new paste ] login | about

Link: http://codepad.org/awZal2nW    [ raw code | output | fork ]

Python, pasted on Jan 10:
import random

repetitions = 10000

doors = [1,2,3]

def experiment(repetitions, montyKnows = True):
	switchWins = 0
	keepWins = 0
	goatReveals = 0
	
	for i in xrange(repetitions):
		# The door containing the car
		carDoor = random.choice(doors)
		
		# The door I pick
		myDoor = random.choice(doors)
		
		# Monty will open one of the two doors I didn't pick
		if montyKnows:
			# If Monty knows which door has the car, he randomly opens one that doesn't.
			montyDoor = random.choice([d for d in doors if d != myDoor and d != carDoor])
			
		else:
			# If he doesn't, he randomly opens one of the two doors
			montyDoor = random.choice([d for d in doors if d != myDoor])
			
			# If it contains a car, we don't count this experiment and try again
			if montyDoor == carDoor:
				continue
		
		# At this point, Monty has opened a door which doesn't contain a car, so the car is either behind my door, or the other closed one
		goatReveals += 1
		if myDoor == carDoor:
			keepWins += 1
		else:
			switchWins += 1
	
	
	if montyKnows:
		print "Monty knew which door contained the car, and intentionally opened one that didn't."
	else:
		print "Monty randomly chose one of the two doors I didn't pick to open."
	
	print "We ran the experiment %s times."%repetitions
	print "Monty revealed a goat %s times."%goatReveals
	print "Switching would have gotten us the car %s times, which is %.1f%% of the times he revealed a goat."%(switchWins, switchWins*100./goatReveals)
	print "Keeping our door would have gotten us the car %s times, which is %.1f%% of the times he revealed a goat."%(keepWins, keepWins*100./goatReveals)


print "Experiment 1:"
experiment(repetitions, montyKnows = True)
print
print "Experiment 2:"
experiment(repetitions, montyKnows = False)


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
Experiment 1:
Monty knew which door contained the car, and intentionally opened one that didn't.
We ran the experiment 10000 times.
Monty revealed a goat 10000 times.
Switching would have gotten us the car 6750 times, which is 67.5% of the times he revealed a goat.
Keeping our door would have gotten us the car 3250 times, which is 32.5% of the times he revealed a goat.

Experiment 2:
Monty randomly chose one of the two doors I didn't pick to open.
We ran the experiment 10000 times.
Monty revealed a goat 6639 times.
Switching would have gotten us the car 3301 times, which is 49.7% of the times he revealed a goat.
Keeping our door would have gotten us the car 3338 times, which is 50.3% of the times he revealed a goat.


Create a new paste based on this one


Comments: