# Prove the Monty Hall Problem (via Simulation)!

• (5 Pages)
• 1
• 2
• 3
• 4
• 5

## 61 Replies - 24338 Views - Last Post: 03 March 2015 - 10:27 AM

### #31 sepp2k

• D.I.C Lover

Reputation: 2242
• Posts: 3,440
• Joined: 21-June 11

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 08:57 AM

raziel_, on 22 September 2012 - 05:13 PM, said:

Maybe im just stupid over here but if one of the door is opened then dont you have 2 choices now?

Yes, you have the choice to stay with your original choice or to switch.

Quote

No. Think of it this way: If you picked one of the two wrong doors at the beginning, you'll always get to the correct door if you switch (because the second wrong door will have been eliminated). If you picked the right door at the beginning, you'll get a wrong door if you switch. So what is the chance that you win if you switch? 2/3 because that's the chance of picking a wrong door at the beginning.

### #32 baavgai

• Dreaming Coder

Reputation: 6198
• Posts: 13,308
• Joined: 16-October 07

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 09:11 AM

Your answer is intuition, and what most people believe, but not math. Indeed, sometimes it's so hard to convince someone of this that you have to write a computer based simulation.

Think of it this way, your first guess has a one in three chance, right? With two in three being the chance of not guessing right. When you switch your answer, you essentially have chosen two out of three, not one out of three.

### #33 jon.kiparsky

• Pancakes!

Reputation: 8576
• Posts: 14,820
• Joined: 19-March 11

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 09:19 AM

raziel_, on 22 September 2012 - 10:13 AM, said:

Maybe im just stupid over here but if one of the door is opened then dont you have 2 choices now? and your chance is 50/50? and hows that if one of the doors is out of the game is still counted as a choice?

You're wrong, but that doesn't make you stupid.

Yes, you have two choices, but the odds are not 50/50 that the prize is behind one of your two choices. Remember when you picked one door from three? When you picked that door, your odds were 1/3, and it was 2/3 that the prize was behind one of the other two doors.

Imagine we play the game a little differently. This time, the host does not open a door, but asks you if you want to take both of the other doors, or the one you started with. What are your odds this time, if you switch? If you stay?

### #34 Skydiver

• Code herder

Reputation: 4026
• Posts: 12,836
• Joined: 05-May 12

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 10:43 AM

I'm beginning to grok the result, but just to make sure, I understand things, I'll ask a slightly different question:

If presented a red pill, a blue pill, and a green pill where one of the pills is poisonous. After I pick a pill, and one other pill is shown to be a placebo, then I should not change my mind. Is this correct?

### #35 jon.kiparsky

• Pancakes!

Reputation: 8576
• Posts: 14,820
• Joined: 19-March 11

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 11:01 AM

That is correct. The odds of you picking the poisoned pill the first time are 1/3. The odds of your not having picked it are 2/3. That means if you switch, you go from 1/3 chance of getting the poison to 2/3 chance.

Of course, someone who presents you with that sort of puzzle is likely to know that you're familiar with this problem and therefore you're into a mindfuck situation. At that point, your best bet is to overpower the son of a bitch and cram all three pills into him, and hold his mouth and nose until he swallows. Your odds are much better in that scenario, assuming you get the advantage of surprise.

### #36 Skydiver

• Code herder

Reputation: 4026
• Posts: 12,836
• Joined: 05-May 12

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 11:22 AM

Where is iocane powder when you need it? (Unfortunately, only a choice between two rather than one out of three.)

This post has been edited by Skydiver: 22 September 2012 - 11:24 AM

### #37 Momerath

• D.I.C Lover

Reputation: 1019
• Posts: 2,462
• Joined: 04-October 09

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 22 September 2012 - 11:26 AM

And this problem is why I can no longer watch Deal Or No Deal. They consistently misstate the odds of having the \$1,000,000 case, and I yell at the TV. My wife doesn't like it when I yell at the TV

And if you are ever on the show and you are down to the last two cases and they ask if you want to switch your case, you say YES!

### #38 foohoo

Reputation: 2
• Posts: 41
• Joined: 10-August 07

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 28 September 2012 - 06:28 AM

The logic works on the fact that your beginning choice has a 2 in 3 chance of being wrong (i.e. the goat). When you get to the second stage and the gameshow host removes a door (which is always a goat i.e he can't remove the good prize) it means, as you have most likely selected a goat in the first stage, the remaining door holds the good prize and you should swap.

If it got to the second stage and the host removed one of the remaining doors at random (so he could actually remove the good prize) then you are back to a 50/50 choice.

- at least this is how I understand it

### #39 jon.kiparsky

• Pancakes!

Reputation: 8576
• Posts: 14,820
• Joined: 19-March 11

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 28 September 2012 - 06:42 AM

foohoo, on 28 September 2012 - 08:28 AM, said:

If it got to the second stage and the host removed one of the remaining doors at random (so he could actually remove the good prize) then you are back to a 50/50 choice.

- at least this is how I understand it

I'm with you up to this part. In this case, you have the same odds of getting the prize whether you switch or stay, but it's not 1/2. There's a 1/3 chance that the prize has been removed, so you have 1/3 chance of getting it if you switch or if you stay.

### #40 TheNemesisFactor

Reputation: 0
• Posts: 2
• Joined: 30-June 13

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 30 June 2013 - 11:59 AM

SO I tried to take a more scientific as opposed to mathematical approach. Instead of always switching and recording the result then always NOT switching and recording, I said let's not worry about whether the contestant switched but instead track whether it was better to switch. Preliminary I used rng to determine which prize was behind which door. I found that this did not truly represent a "random seeming" set due to the small set of numbers to choose from, thus skewing the results. I instead used a Fisher-Yates shuffle, which got more consistently random door:prize permutations. That being said, there's been a lot of conversation about the mathematical solution, and we can logic out that you really have a 50% chance of winning, as you really only have two choices (since one is eliminated for you) and one MUST be the win. My particular simulation proves that it is slightly better to switch (If your first choice is based on the result of a rng). However, none of the simulations can take into account the emotional state of a mind during the decision process. Does the contestant have a certain affinity for the number 3, or for odd numbers, or does the game take place on the first of January? That draws the conclusion that regardless of the scientific results, I think we can all agree it is only better to switch if you didn't choose the car in the first place, but only a Sith deals in absolutes and that defeats the purpose of writing cool code to figure it out

Results after one billion contests:
Number Of Simulations: 100000000
Times better to keep choice: 497868337
Times better to switch choice: 502131663

Code:
``` public MOntyHall()
{
while (true)
{
for (int t = 0; t < 1000000000; ++t)
DoOneSim();
DisplayResults();
}
}

public void DoOneSim()
{
NumSims++;
var Prizes=new List<int>();
var Doors=new List<int>();
for(int t=1;t<4;++t)
{
}
Prizes.Shuffle();
Doors.Shuffle();
var DoorPrizes=new Dictionary<int,int>();
for(int t=0;t<3;++t)
{
var whichDoor=random.Next(0,Doors.Count-1);
var whichPrize=random.Next(0,Prizes.Count-1);
Doors.RemoveAt(whichDoor);
Prizes.RemoveAt(whichPrize);
}
var contestantSelection=random.Next(1,3);
var remainingDoors=DoorPrizes.Where(x=>x.Key!=contestantSelection).Select(x=>x.Key).ToList();
var removedSelection = 0;
if (DoorPrizes[contestantSelection]==3)
removedSelection = remainingDoors.First(x => x != DoorPrizes.First(x2=>x2.Value==3).Key);
else
removedSelection = remainingDoors[random.Next(0, 1)];
var remainingSelection = remainingDoors[removedSelection == remainingDoors[0] ? 1 : 0];
if (DoorPrizes[remainingSelection] == 3)
NumSwitchBetter++;
else
NumStayBetter++;
}

public void DisplayResults()
{
Console.Clear();
Console.WriteLine("Number Of Simulations:          " + NumSims.ToString());
Console.WriteLine("Times better to keep choice:    " + NumStayBetter.ToString());
Console.WriteLine("Times better to switch choice:  " + NumSwitchBetter.ToString());
}

}

public static class Extensions
{
public static void Shuffle<T>(this IList<T> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
```

### #41 Momerath

• D.I.C Lover

Reputation: 1019
• Posts: 2,462
• Joined: 04-October 09

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 30 June 2013 - 01:43 PM

Your code be broken. This line
```var whichDoor=random.Next(0,Doors.Count-1);
```
limits your door choice to 0 or 1. Random.Next(x,y) does from x (inclusive) to y (exclusive). Remove the -1 (on both your Random.Next statements) and try again.

### #42 TheNemesisFactor

Reputation: 0
• Posts: 2
• Joined: 30-June 13

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 30 June 2013 - 03:12 PM

Momerath, on 30 June 2013 - 01:43 PM, said:

Your code be broken. This line
```var whichDoor=random.Next(0,Doors.Count-1);
```
limits your door choice to 0 or 1. Random.Next(x,y) does from x (inclusive) to y (exclusive). Remove the -1 (on both your Random.Next statements) and try again.

Ah the infamous off by one bug! The new results suggest 66% chance of winning if you stick with your original choice. Interestingly enough the answer remains between 66.4% and 66.7%. What I just realized, though, is I allowed the host to remove the car if the contestant didn't choose it in the first place. If the host is not allowed to remove the car, the stats reverse (66% chance it is better to keep your original choice).

This makes more sense. If it is better to stay when you first choose the car, and you will choose the car 33% of the time, then 66% of the time you didn't choose the car and the remaining choice after a goat is eliminated will be the car.

Then I ran three more simulations (contestant always chooses 1, always chooses 2, always chooses 3). This had no bearing on the result and throws out my concerns over the simulation being unrealistic. The results show that it doesn't matter what door you pick, only what's behind it. That might seem a little esoteric, but obvious once stated.

### #43 Momerath

• D.I.C Lover

Reputation: 1019
• Posts: 2,462
• Joined: 04-October 09

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 02 July 2013 - 03:11 PM

TheNemesisFactor, on 30 June 2013 - 03:12 PM, said:

Ah the infamous off by one bug! The new results suggest 66% chance of winning if you stick with your original choice.

If the results suggest that, then you have another bug. Not going to find it for you this time.

### #44 cfoley

• Cabbage

Reputation: 2171
• Posts: 4,559
• Joined: 11-December 07

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 02 July 2013 - 03:34 PM

Quote

If the results suggest that, then you have another bug.

This is actually a huge problem in home-grown scientific software. The scientists often don't have the skills to test properly or the knowledge to version control properly. It's rare to find results that reference the software version, let alone the precise build.

### #45 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4734
• Posts: 8,394
• Joined: 08-June 10

## Re: Prove the Monty Hall Problem (via Simulation)!

Posted 03 July 2013 - 06:20 AM

Yep, that's why (I think) this is a fun challenge. Mathematically and statistically speaking, we already know the answer. The goal here isn't to try to find an answer; it's to simulate and prove the existing answer. You get to exercise both your programming and statistics skills.