Monty Hall simulation in python

The Monty Hall problem, or paradox as it is sometimes known, has always intrigued me, so much so that I wrote a python based simulator to prove it to be true or false.

The crux of it, and as its name implies, is that you are on the old “Let’s Make A Deal” show, and as usual, Monty offers you a choice of three doors. You select one, and he then proceeds to open one of the other doors, and *always* reveals a goat, as he knows what is behind each door. At that point, he offers you the chance to switch your choice to the other remaining door. Why you would want to do this, and as we will show, you do want to do this, is that you only win 1/3 of the time when you choose. However, as soon as Monty removes one of the doors from your choices, you have immediately increased your chances of winning, as 2/3 of the time, you choose the wrong door.

The easiest way to think of it is this:

As noted, you choose incorrectly 1/3 of the time. By default, that means you would choose correctly if you chose the other two doors. Since Monty eliminates one of the doors, you still win by switching 2/3 of the time. The only time you don’t win by switching is when you chose correctly in the first place, which is 1/3 of the time.

Our program is below, and you should be able to run it with any version of python.

import random

def montyHall(choice):
  l = []
  n = random.randint(1,100)
  if n <= 67 and n > 33:
    l.append("goat")
    l.append("car")
    l.append("goat")
  elif n > 67:
    l.append("goat")
    l.append("goat")
    l.append("car")
  else:
    l.append("car")
    l.append("goat")
    l.append("goat")

  ar = choice
  print "you chose door number ", choice
  j = ""
  for i in range(3):
    if i != int(ar) - 1:
      if l[i] != "car":
        j = i
        print " i have removed choice number ", i + 1
        break

  for i in range(3):
    if i != int(ar) - 1 and i != j:
      if l[i] != "car":
        return -1
      else:
        return 1

record = dict()
record["wins"] = 0
record["losses"] = 0
for i in range(1000):
  n = random.randint(1,3)
  result = montyHall(n)
  if result == 1:
    record["wins"] = record["wins"] + 1
  else:
    record["losses"] = record["losses"] + 1
print record

When run with 1,000 simulations, we inevitably see that 2/3 of the time, switching would allow you to win.

Below we print the output of the first five and last five simulations, but the totals represent all 1,000.

-bash-4.1$ python montyHall.py | awk '{if (NR <= 10 || NR >= 1990) {print}}'
you chose door number  1
 i have removed choice number  2
you chose door number  1
 i have removed choice number  3
you chose door number  2
 i have removed choice number  1
you chose door number  1
 i have removed choice number  2
you chose door number  1
 i have removed choice number  2
you chose door number  1
 i have removed choice number  2
you chose door number  2
 i have removed choice number  3
you chose door number  3
 i have removed choice number  1
you chose door number  3
 i have removed choice number  2
you chose door number  3
 i have removed choice number  1
{'wins': 670, 'losses': 330}
-bash-4.1$

1 comment for “Monty Hall simulation in python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.