The Monty Hall Problem

I was watching MythBusters and they visited this problem which at Seneca we discussed briefly as well (I might have done this one before but thought I’d repost it anyway):

  1. There are 3 doors, 2 are losers and 1 is a winner
  2. You pick a door (2/3 chance it’s a loser)
  3. They show you the other loser door (leaving the winner door [1/3 chance] and the loser door [2/3 chance])
  4. You decide to switch your door (since it was 2/3 likely you originally chose a losing door and they took the other losing door away, switching increases your odds since the other door is likely to be the winner door)
  5. You win! (still a small chance of losing though)

$ python monty.py 0 100 1

('Doors:', {1: 'win', 2: 'lose', 3: 'lose'})
('Pick:', 1, 'win')
('Show:', 2, 'lose')
('Final:', 1, 'win')

('Doors:', {1: 'lose', 2: 'lose', 3: 'win'})
('Pick:', 2, 'lose')
('Show:', 1, 'lose')
('Final:', 2, 'lose')

('Doors:', {1: 'lose', 2: 'lose', 3: 'win'})
('Pick:', 1, 'lose')
('Show:', 2, 'lose')
('Final:', 1, 'lose')


('Wins:', 32, '/', 100, '=', 0.32)

$ python monty.py 1 100 1

('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})
('Pick:', 2, 'win')
('Show:', 1, 'lose')
('Switching:', 2, 'to', 3)
('Final:', 3, 'lose')

('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})
('Pick:', 3, 'lose')
('Show:', 1, 'lose')
('Switching:', 3, 'to', 2)
('Final:', 2, 'win')

('Doors:', {1: 'lose', 2: 'win', 3: 'lose'})
('Pick:', 1, 'lose')
('Show:', 3, 'lose')
('Switching:', 1, 'to', 2)
('Final:', 2, 'win')


('Wins:', 74, '/', 100, '=', 0.74)
import random
import sys
try:
	switch = int(sys.argv[1])
except:
	switch = 0
try:
	rounds = int(sys.argv[2])
except:
	rounds = 0
try:
	debug = int(sys.argv[3])
except:
	debug = 0
wins = 0
for x in range(0, rounds):
	doors = ["lose", "lose", "win"]
	random.shuffle(doors)
	doors = {1:doors[0], 2:doors[1], 3:doors[2]}
	if (debug != 0):
		print("Doors:", doors)
	pick = random.randint(1, 3)
	if (debug != 0):
		print("Pick:", pick, doors[pick])
	show = 1
	for k in doors.keys():
		if ((k != pick) and (doors[k] == "lose")):
			show = k
			break
	if (debug != 0):
		print("Show:", show, doors[show])
	if (switch != 0):
		for k in doors.keys():
			if ((k != pick) and (k != show)):
				other = k
				break
		print("Switching:", pick, "to", other)
		pick = other
	print("Final:", pick, doors[pick])
	if (doors[pick] != "lose"):
		wins += 1
	if (debug != 0):
		print("")
print("")
print("Wins:", wins, "/", rounds, "=", float(wins) / float(rounds))

Advertisements
The Monty Hall Problem

One thought on “The Monty Hall Problem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s