コード例 #1
0
ファイル: Program.cs プロジェクト: jcnoyes/Coding_Practice
        //main program, starts here
        static void Main(string[] args)
        {
            bool            win = false;
            bool            continueExecution = true;
            int             numberOfWinnings  = 0;
            simulation      sim = new simulation();
            randomNumberGen rg  = new randomNumberGen();

            //get the consoleInterface, display welcome screen
            consoleInterface ic = consoleInterface.getInterface();

            ic.displayWelcome();

            //conitnue while user wants to
            while (continueExecution == true)
            {
                numberOfWinnings = 0;
                //start simulation of not switching doors
                ic.startWithoutSwitching();

                for (int i = 0; i < 10000; i++)
                {
                    int seed = rg.getNumber(100, i + Environment.TickCount);
                    win = sim.runSimWithoutSwitching(seed);

                    if (win == true)
                    {
                        numberOfWinnings += 1;
                    }
                }

                //calculate ratio which should be around 1/3
                double withoutSwitchingRatio = numberOfWinnings / 10000.0;

                //Displays results and resets numberOfWinnings variable
                ic.resultsWithoutSwitching(numberOfWinnings, withoutSwitchingRatio);
                numberOfWinnings = 0;

                //start simulation with switching
                ic.startWithSiwtching();

                for (int i = 0; i < 10000; i++)
                {
                    int seed = rg.getNumber(100, i + Environment.TickCount);
                    win = sim.runWithSwitching(seed);

                    if (win == true)
                    {
                        numberOfWinnings += 1;
                    }
                }

                //calculate ratio which should be around 2/3
                double withSwitchingRatio = numberOfWinnings / 10000.0;

                //Displays results
                ic.resultsWithSwitching(numberOfWinnings, withSwitchingRatio);
                continueExecution = ic.compareRatio(withoutSwitchingRatio, withSwitchingRatio);
            }
        }
コード例 #2
0
        public bool runSimWithoutSwitching(int s)
        {
            //boolean to see if you won.
            bool            win     = false;
            randomNumberGen randGen = new randomNumberGen();

            //first pick the winning door, and user choice
            int winningDoor = randGen.getNumber(4, s);
            int userChoice  = randGen.getNumber(4, s + s);

            if (winningDoor == userChoice)
            {
                win = true;
            }

            return(win);
        }
コード例 #3
0
        public bool runWithSwitching(int s)
        {
            bool            win     = false;
            randomNumberGen randGen = new randomNumberGen();

            //pick winning door
            int winningDoor = randGen.getNumber(4, s);
            int userChoice  = randGen.getNumber(4, s + s);
            int doorShown   = 0;

            List <int> losingDoors = new List <int>();

            //place losing doors in array, so Monte Hall will know them
            for (int i = 1; i < 4; i++)
            {
                if (i != winningDoor)
                {
                    losingDoors.Add(i);
                }
            }

            //Monte Hall displays a losing door
            if (userChoice == losingDoors[0])
            {
                //door shown = losingDoors[1]
                doorShown = losingDoors[1];
            }

            else
            {
                //door shown = losingDoor[0]
                doorShown = losingDoors[0];
            }

            //switch door to door that was not shown
            if (doorShown == 2 && userChoice == 1)
            {
                userChoice = 3;
            }
            else if (doorShown == 2 && userChoice == 3)
            {
                userChoice = 1;
            }
            else if (doorShown == 1 && userChoice == 2)
            {
                userChoice = 3;
            }
            else if (doorShown == 1 && userChoice == 3)
            {
                userChoice = 2;
            }
            else if (doorShown == 3 && userChoice == 1)
            {
                userChoice = 2;
            }
            else if (doorShown == 3 && userChoice == 2)
            {
                userChoice = 1;
            }

            //see if user won
            if (userChoice == winningDoor)
            {
                win = true;
            }

            return(win);
        }