By the way, I didn't optimize it to do dynamic doors, only 3 doors, since I think if you have a million doors and 999,998 were opened for you it's the same scenario with just 3 doors. That's what I guess, anyway.

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

### #16

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

Posted 20 September 2012 - 01:29 AM

### #17

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

Posted 20 September 2012 - 02:30 AM

Proved using an exhaustive search of 9 runs:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExhaustiveMontyHallProblem { class ExhaustiveGoatSearch { private int doorCount; public ExhaustiveGoatSearch(int doorCount) { this.doorCount = doorCount; } private void runSimulations() { int changeCount = 0; int keepCount = 0; int gameCount = 0; for (int i = 0; i < doorCount; i++) { for (int j = 0; j < doorCount; j++) { gameCount++; Game game = new Game(doorCount, i, j); Console.WriteLine(game.toString()); if (game.shouldChangeDoors()) { changeCount++; } else { keepCount++; } } } String bestAction = changeCount > keepCount ? "always" : "never"; Console.WriteLine("The player should have changed doors " + changeCount + " times."); Console.WriteLine("The player should have kept doors " + keepCount + " times."); Console.WriteLine("The best strategy is to " + bestAction + " change doors."); } static void Main(string[] args) { new ExhaustiveGoatSearch(3).runSimulations(); Console.ReadKey(); } } class Game { private int doorCount; private int carPosition; private int initialChoice; public Game (int doorCount, int carPosition, int initialChoice) { this.carPosition = carPosition; this.initialChoice = initialChoice; this.doorCount = doorCount; } public bool shouldChangeDoors() { return carPosition != initialChoice; } public String toString() { String result = ""; for(int i = 0; i < doorCount; i++) { if (i == carPosition) { result += "Car "; } else { result += "Goat "; } } result += "-- Initial choice is door " + (initialChoice + 1); if (shouldChangeDoors()) { result += ". Player should change doors."; } else { result += ". Player should not change doors."; } return result; } } }

### #18

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

Posted 20 September 2012 - 11:29 AM

I've not looked closely at fromTheSprawl's code, but I'm puzzled by the results he is getting. As predicted, by not swapping, chance of winning was only about 33%. No problem there. I thought that by swapping, the contestant had 66% chance of winning, but his numbers show barely 50%. As a quick sanity check, I ran Curtis Rutland's code and that was declaring around a 66% chance of winning.

### #20

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

Posted 20 September 2012 - 12:48 PM

No one took me up on the LINQ challenge? Oh well, curiosity won. Seems it is similar:

Spoiler

### #21

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

Posted 20 September 2012 - 12:50 PM

I honestly tried, baavgai, but I don't understand Python well enough to understand what was going on in your program.

### #22

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

Posted 20 September 2012 - 12:59 PM

For the 50% on fromTheSprawl's code...

Then, um, read the LINQ?

if switchOk == "yes": doorNumber = randint(0,2) # one goatDoor is chosen # so that's is out of the mix # however, we ignore doorNumber passed # and randomly choose one of the two remaining doors # one wins, one loose, hence 50% while doorNumber == goatDoor: doorNumber = randint(0,2)

Curtis Rutland, on 20 September 2012 - 03:50 PM, said:

I honestly tried, baavgai, but I don't understand Python well enough to understand what was going on in your program.

Then, um, read the LINQ?

### #23

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

Posted 20 September 2012 - 01:01 PM

baavgai, on 20 September 2012 - 02:59 PM, said:

Then, um, read the LINQ?

That's what I was working on

Here's a shortened version of your own code using the extension methods (that I find cleaner):

var total = Enumerable.Range(0, runs) .Where(i => rnd.Next(0, 3) != rnd.Next(0, 3)).Count();

Side note: Resharper bitches about rnd.Next(0,3) != rnd.Next(0,3) and wants to replace it with false. I guess it's not smart enough to realize that the function returns different values when provided the same arguments.

### #24

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

Posted 20 September 2012 - 01:14 PM

A breakdown.

Spoiler

### #25

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

Posted 20 September 2012 - 06:23 PM

baavgai yeah when I was going home I remembered there's a serious flaw in my montyhall function since even if I set it to switch there's a chance the first chosen door will still be chosen so here's to recompense:

Results are in, not as big a difference as before but it's still there:

In the process I get to learn list comprehensions in Python and a lot of beginner errors.

Spoiler

Results are in, not as big a difference as before but it's still there:

Spoiler

In the process I get to learn list comprehensions in Python and a lot of beginner errors.

### #26

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

Posted 20 September 2012 - 08:27 PM

That's still incorrect. Switching should yield 66% wins, as proven by other simulations and math.

### #27

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

Posted 20 September 2012 - 09:57 PM

Hmm, okay I'll work on it!

### #28

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

Posted 20 September 2012 - 10:23 PM

fromTheSprawl, on 20 September 2012 - 08:23 PM, said:

In the process I get to learn list comprehensions in Python and a lot of beginner errors.

That's a worthy endeavor.

Here's a way to think about it that might help:

Three doors, A, B, C. I choose A. Host opens B, says "do you want to switch?".

Misconception: I am choosing between door A and door C. (this is in your program)

Actual case: I am choosing between "the prize is behind door A" and "The prize is behind door B or door C, and we know it's not behind door B".

It's okay if you get confused with this. I know this problem well, and I woke up this morning convinced I'd done it wrong.

### #29

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

Posted 20 September 2012 - 10:38 PM

Good grief!

And the new results:

How about now?

Yes Jon, that was the problem of my first code yesterday. I went the whole way home thinking about that simple but fatal mistake, and then the second one's mistake too. It's the little things that gets you huh.

Spoiler

And the new results:

Spoiler

How about now?

Yes Jon, that was the problem of my first code yesterday. I went the whole way home thinking about that simple but fatal mistake, and then the second one's mistake too. It's the little things that gets you huh.

### #30

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

Posted 22 September 2012 - 08:13 AM

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?