The Monty Hall problem is a probabilistic puzzle that confounds lots of
people including a Nobel laurate in physics apparently!
SAMPLES=1000000
open_door=random.randint(1,3)
if door==player:
correct_stick=correct_stick+1
else:
correct_swap=correct_swap+1
The Monty Hall problem is based on a TV quiz show and is stated in this
way: A player is presented with three doors and behind one of the doors is a
prize. The aim of the game is for the player to select the door with the prize
behind. Of course, the player does not know which of the doors the prize is
behind. The player guesses which of the
three doors the prize is behind. At that point one of the doors which the prize
is not behind and which has not been selected by the player is opened. So now
there are two closed doors. The player is then asked whether they wish to stick
with the door that they selected or switch to the other remaining closed door.
Intuitively we think of the problem like this. Originally there is a one in three chance of
us selecting the correct door. Then one of
the doors is opened and then it appears the problem becomes a fifty-fifty choice between the two remaining closed doors so it does
not matter whether we switch or not. But this thinking is incorrect. The answer is the player should always switch
because they have twice the chance of winning the prize than if they stick to
the door they chose originally.
There are lots of mathematical solutions that demonstrate this on the
web. Here we show a solution using a simple program. IF you run the code below you will find that two
thirds of the time you will win the prize if you switch, but if you stick you
will win the prize on one in three occasions. Running the code reveals to us what intuitively we cannot see.
Further Reading
https://en.wikipedia.org/wiki/Monty_Hall_problemCode
import random
correct_swap=0
correct_stick=0SAMPLES=1000000
# run lots of times so we have a statistically valid result
for i in range(SAMPLES):
# select a random door for the prize to be
behind
door=random.randint(1,3)
# player makes a selection;
player=random.randint(1,3)
# Open a door that neither has the prize
behind
# and has not been selected by the player
open_door=random.randint(1,3)
while open_door==door or open_door==player:
open_door=random.randint(1,3)
# Choose whether to stick or switch
option=random.choice(['stick','swap'])
# stick with the same door
if option=="stick":if door==player:
correct_stick=correct_stick+1
else:
correct_swap=correct_swap+1
# swap door
elif option=="swap":
# switch to the other unopened door
if open_door==1 and player==2: player=3
elif open_door==1 and player==3:
player=2
elif open_door==2 and player==1:
player=3
elif open_door==2 and player==3:
player=1
elif open_door==3 and player==2:
player=1
elif open_door==3 and player==1:
player=2
if door==player:
correct_swap=correct_swap+1
else:
correct_stick=correct_stick+1
print("Stick: ",
correct_stick/SAMPLES*100)
print("Swap:",
correct_swap/SAMPLES*100)
Comments
Post a Comment