probability - Monty Hall Program Simulation (C#) -


i trying simulate monty hall problem (because read book think statistics guy in particular convinced after seeing computer simulation) in c#, programming language familiar me. scenario such position of prize random (in each run), choice random, , game host's choice of opening door random (it can't random if picked non-prize).

surprisingly though, program achieves result of 50:50 chance of winning no matter whether switch or not. here's code (pardon me lengthiness):

class program {     static void main(string[] args)     {         random rand = new random();          int noswitchwins = rungames(rand, false, 10000);         int switchwins = rungames(rand, true, 10000);          console.writeline(string.format("if don't switch, win {0} out of 1000 games.", noswitchwins));         console.writeline(string.format("if switch, win {0} out of 1000 games.", switchwins));          console.readline();     }      static int rungames(random rand, bool doswitch, int numberofruns)     {         int counter = 0;          (int = 0; < numberofruns; i++)         {             bool iswin = rungame(rand, doswitch);             if (iswin)                 counter++;         }          return counter;     }      static bool rungame(random rand, bool doswitch)     {         int prize = rand.next(0, 2);         int selection = rand.next(0, 2);          // available choices         list<choice> choices = new list<choice> { new choice(), new choice(), new choice() };         choices[prize].isprize = true;         choices[selection].isselected = true;         choice selectedchoice = choices[selection];         int randomlydisplayeddoor = rand.next(0, 1);          // 1 of choices displayed         var choicestodisplay = choices.where(x => !x.isselected && !x.isprize);         var displayedchoice = choicestodisplay.elementat(choicestodisplay.count() == 1 ? 0 : randomlydisplayeddoor);         choices.remove(displayedchoice);          // switch?         if (doswitch)         {             choice initialchoice = choices.where(x => x.isselected).firstordefault();             selectedchoice = choices.where(x => !x.isselected).firstordefault();             selectedchoice.isselected = true;         }          return selectedchoice.isprize;     } }  class choice {     public bool isprize = false;     public bool isselected = false; } 

this entirely own interest's sake, , wrote in way familiar , comfortable me. feel free offer own opinion , critique, thank much!

rand.next(0,2) 

only returns 0 or 1; upper bound exclusive. never picking third door (unless switch), , third door never has prize. modelling wrong problem.

try instead:

rand.next(0,3) 

likewise:

int randomlydisplayeddoor = rand.next(0, 1); 

only ever selects first of candidate doors; should be:

int randomlydisplayeddoor = rand.next(0, 2); 

now get:

if don't switch, win 3320 out of 1000 games. if switch, win 6639 out of 1000 games. 

note - upper bound inclusive when equals - i.e. rand.next(1,1) returns 1.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -