60 Replies  17131 Views  Last Post: 16 June 2014  07:20 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
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
#20
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  12:48 PM
#21
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  12:50 PM
#22
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  12:59 PM
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:
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:
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
#25
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  06:23 PM
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.
#26
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  08:27 PM
#27
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 20 September 2012  09:57 PM
#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:
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
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.
#30
Re: Prove the Monty Hall Problem (via Simulation)!
Posted 22 September 2012  08:13 AM
