After spending several agonizing hours on the bed unable to sleep, I decided at 5 in the morning to create a simple simulation for the Monty Hall problem.
The Monty Hall problem is a popular probability puzzle based on the game show 'Let's make a deal' and is named after it's original host, Monty hall.
The Problem in essence is described below.
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?
The wiki to the problem can be found here.
The code I wrote can be viewed and simulated online here by pressing the run button.
The simulation shows that the participant wins approximately 67% or 2/3 of the time.
A simple description and solution to the problem.
import random
win = 0
loss = 0
def game_logic():
global win,loss
for i in range(1,10000,1):
l1 = ["car","goat","goat"]
random.shuffle(l1)
t1 = random.randint(0,2)
player_choice = l1[t1]
l1.pop(t1)
t2 = random.randint(0,1)
door_open = l1[t2]
if(door_open == "car"):
if (t2 == 0):
t2 = 1
door_open = l1[t2]
else:
t2 = 0
door_open = l1[t2]
else:
pass
l1.pop(t2)
player_choice = l1[0]
if (player_choice == "car"):
win += 1
else:
loss += 1
print "win:" + str(win)
print "loss:" + str(loss)
print "percentage wins:" + str(win/100)
game_logic()
## Code ends
Neat huh ? Python rocks !
The Monty Hall problem is a popular probability puzzle based on the game show 'Let's make a deal' and is named after it's original host, Monty hall.
The Problem in essence is described below.
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?
The wiki to the problem can be found here.
The code I wrote can be viewed and simulated online here by pressing the run button.
The simulation shows that the participant wins approximately 67% or 2/3 of the time.
A simple description and solution to the problem.
The Code in Python:
## Code startsimport random
win = 0
loss = 0
def game_logic():
global win,loss
for i in range(1,10000,1):
l1 = ["car","goat","goat"]
random.shuffle(l1)
t1 = random.randint(0,2)
player_choice = l1[t1]
l1.pop(t1)
t2 = random.randint(0,1)
door_open = l1[t2]
if(door_open == "car"):
if (t2 == 0):
t2 = 1
door_open = l1[t2]
else:
t2 = 0
door_open = l1[t2]
else:
pass
l1.pop(t2)
player_choice = l1[0]
if (player_choice == "car"):
win += 1
else:
loss += 1
print "win:" + str(win)
print "loss:" + str(loss)
print "percentage wins:" + str(win/100)
game_logic()
## Code ends
Neat huh ? Python rocks !