Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 [but the door is not opened], 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?

This is Monty Hall problem, and we know that a switching strategy really does win two out of three times on the average. But is it possible to simulate it in Matlab?

share|cite|improve this question
It is. What are your problems in simulating that - do you want somebody to write you the code? – Ilya May 31 '12 at 12:17
I mean is it convincing? And is it practicable? – Charles Bao May 31 '12 at 12:18
It is (both convincing and practicable) – Ilya May 31 '12 at 12:38
It might be even more convincing to sit down and think about the problem for 2 minutes. Most likely you don't even have to write the code afterwards any more. – Fabian May 31 '12 at 12:51
@Fabian: it may be faster to write a code :) – Ilya May 31 '12 at 13:03

Some pseudocode:

  1. Set winswap = 0, winnoswap = 0 and simcount = 0.
  2. Increment simcount by 1.
  3. Set car = random value from a choice of $\{ 1, 2, 3\}$.
  4. Set guess = random value from a choice of $\{1, 2, 3\}$.
  5. Set goat = random value from a choice of $\{1, 2, 3\}$ that is not equal to either car or guess.
  6. Set swap = random value from a choice of $\{1, 2\}$.
  7. If swap = 2, set guess = the first value of $\{1, 2, 3\}$ that is not equal to either goat or guess.
  8. If guess = car and swap = 1, increment winnoswap by 1.
    If guess = car and swap = 2, increment winswap by 1.
  9. If simcount is below some predetermined value, return to step 2.
  10. Compare the values of winswap and winnoswap.
share|cite|improve this answer

After the interesting Mythbusters episode on the problem, I wrote a bit of Python code to simulate the results for fun. The code can be simplified considerably by breaking it into two trials, one where you always stay with your choice, and the other where you always swap. A further simplification can be had with the following two observations:

  1. If you always keep your choice, you only win when you select the car (guess == car)
  2. If you always swap, you only win when you do not select the car (guess != car)

This allows one to avoid computing the positions of the goats and the door Monty decides to remove. I am not really familiar with MATLAB, so I will just include the Python code below, but I hope it gives the basic idea for porting to your language of choice.

import random
import sys

def main():
  if len(sys.argv) < 2:
    print "usage: trials"

  t = int(sys.argv[1])

  # Stick with door
  stick_winners = 0
  for i in xrange(t):
    car = random.randint(1, 3)
    choice = random.randint(1, 3)
    if car == choice:
      stick_winners += 1
  print "(stick) won:", stick_winners, "out of", t, float(stick_winners) / float(t)

  # Switch door
  switch_winners = 0
  for i in xrange(t):
    car = random.randint(1, 3)
    choice = random.randint(1, 3)
    # If car != choice, open a goat door and switch, win the car
    if car != choice:
      switch_winners += 1
  print "(switch) won:", switch_winners, "out of", t, float(switch_winners) / float(t)

if __name__ == "__main__":
share|cite|improve this answer
Your code contains a nice way to think about the problem. Under the switch strategy you win the car if and only if your original choice was not the car, making it clear what the probability is. – m_t_ May 31 '12 at 18:10

Code I created based off of Peter Phipps pseudocode for school assignment (very helpful by the way):

n = int(input("Enter the number of iterations: "))

winswap = 0.0
winnoswap = 0.0
simcount = 0.0

for i in range(n):
    simcount = simcount + 1
    picked_door = random.randint(1,3)
    car = random.randint(1,3)
    g = random.randint(1,3)

    completed = False 
    while not completed:
        if g != car and g != picked_door:
            completed = True
            g = random.randint(1,3)      

    swap = random.randint(1,2)

    if swap == 2:
        if g != 1 and picked_door != 1:
            picked_door = 1
        elif g != 2 and picked_door != 2:
            picked_door = 2
            picked_door = 3 

    if picked_door == car and swap == 1:
        winnoswap = winnoswap + 1
    if picked_door == car and swap == 2:
        winswap = winswap + 1

print "The amount of no swap wins is", winnoswap
print "The amount of swap wins is", winswap

total_wins = winnoswap + winswap

percentswap = (winswap/total_wins)*100
percentnoswap = (winnoswap/total_wins)*100

print "Percentage wins of swapping is", percentswap,"%"
print "Percentage wins of not swapping is", percentnoswap,"%"
share|cite|improve this answer

I have created a general Monty Hall simulation here.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.