View Single Post
Old 16-08-2009, 21:39   #55
Damien
Remoaner
Cable Forum Team
 
Damien's Avatar
 
Join Date: Mar 2004
Posts: 32,796
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Damien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver blingDamien has a lot of silver bling
Re: Programming Challenges?

Quote:
Originally Posted by Fobic View Post
Interesting code: made me think .... always a good thing.
Liked your shortcut for resetting the list.

However, i dont think the code works the way it should for the case when the contestant changes their choice.



You seem to be removing the first door you come across with a Goat behind it (which may even be the contestant's chosen door) and then choosing at random from the 2 remaining doors, rather than swapping to the unchosen door.

I suspect this will give results of 33% and 50% in your output which is not the correct answer.
I think 33% and 50% are the correct answers. If they choose to change they have a 50% chance of winning the car

Wait just figured it out. Tis Wrong.

---------- Post added at 21:39 ---------- Previous post was at 21:00 ----------

The CORRECT Answer:

Spoiler: 
Code:
 class Program
    {
        const string CAR = "CAR";
        const string GOAT = "GOAT";

        static void Main(string[] args)
        {
            Random random = new Random();
            Double DidntChangeSuccessRate = 0;
            Double DidChangeSuccessRate = 0;
            Int32 doorToOpen;
            Int32 IndexOfCar;
            Int32 doorForHostToOpen;

            IList<String> doors = new List<String>()
            {
                    GOAT, GOAT, GOAT
            };

            for (Int32 round = 1; round <= 10000; round++)
            {

                doors[random.Next(0, 3)] = CAR; //Randomly assign a car door (thus killing a goat. Happy Times)
                IndexOfCar = doors.IndexOf(CAR); //The Host needs to know which is the car door
                doorToOpen = random.Next(3); //Choose a Door 
                doorForHostToOpen = random.Next(3); //Host Chooses door to open

               //Now a door has been chosen Noel 'I believe in rubbish' Edmonds needs to remove a door
                //Choose the one that is not a car and not one the user has chosen.
                while (doorForHostToOpen == IndexOfCar || doorForHostToOpen == doorToOpen)
                    doorForHostToOpen = random.Next(3);


                if (round % 2 == 0) //Is Even do a switch, otherwise do a didn't switch 
                {
                    //Switch door. So choose the door that hasn't been opened and was not the 
                    //previously chosen one. 
                    Int32 SwitchedDoor = random.Next(3);
                    while (SwitchedDoor == doorForHostToOpen || SwitchedDoor == doorToOpen)
                        SwitchedDoor = random.Next(3);

                    if (doors[SwitchedDoor] == CAR) //Did they get the car
                        DidChangeSuccessRate++;

                }
                else
                {
                    if (doors[doorToOpen] == CAR) //Don't change, did they get the car. 
                        DidntChangeSuccessRate++;
                }

                //Reset car door to goat. Faster than creating new list class each time
                doors[doors.IndexOf(CAR)] = GOAT;
            }

            Console.WriteLine(String.Format("Didn't Change: {0}%", (DidntChangeSuccessRate / 5000) * 100));
            Console.WriteLine(String.Format("Did Change: {0}%", (DidChangeSuccessRate / 5000) * 100));
            Console.ReadKey(true); //And so we end our magical story. The Code goes on. 
        }


Didn't change: 33.5% chance of winning
Did Change: 66.78% chance of winning

Also got to remove the nasty loop adding/removing. Bonus points for spotting the Beatles reference.
Damien is offline   Reply With Quote