Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have seen complicated solutions, this is an easy one (for always switching) and showed a result of (2/3) winning when switching, would you please tell me how my code can be edited to be better if possible? It's understood that when we see that the possibility of the player winning when switching is (2/3) so the remainder is for not switching is 1-(2/3)=(1/3), or we can write a code far that.

This is the problem's description:
en.wikipedia.org/wiki/Monty_Hall_problem
youtube.com/watch?v=4Lb-6rxZxx0

What I have tried:

    int wins = 0;
    for (int i = 0; i < 1000000; i++)
    {
        if (alwaysSwitch())
        {
            wins++;
        }
    }
    MessageBox.Show(wins.ToString());
}

Random RN = new Random();

bool alwaysSwitch()
{
    int leftGoat = -1, rightGoat = -1, winDoor = -1;

    switch (RN.Next(3))
    {
        case 0: winDoor = 0; leftGoat = 1; rightGoat = 2; break;
        case 1: winDoor = 1; leftGoat = 0; rightGoat = 2; break;
        case 2: winDoor = 2; leftGoat = 0; rightGoat = 1; break;
    }

    int decision = RN.Next(3);

    if (leftGoat == decision)
    {
        rightGoat = -1;
    }

    if (rightGoat == decision)
    {
        leftGoat = -1;
    }

    if (winDoor == decision)
    {
        return false;
    }
    else
    {
        return true;
    }
}
Posted
Updated 17-Apr-20 22:33pm
v3
Comments
Patrice T 17-Apr-20 15:09pm    
Give a link to the site of this problem.
john1990_1 17-Apr-20 15:10pm    
https://en.wikipedia.org/wiki/Monty_Hall_problem
https://www.youtube.com/watch?v=4Lb-6rxZxx0
Patrice T 17-Apr-20 15:15pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
john1990_1 17-Apr-20 15:33pm    
Thx, done.
Richard MacCutchan 18-Apr-20 4:05am    
You return true if the choice is not the winning door; should that not be the other way round? Also why are you setting the left and right goats to -1 if they are selected, since you do not do anything with those values?

If you assume that the player will always switch from their original decision, then they will have selected one of only two doors, since the third one has been opened.

1 solution

There is only one way to lose if you always swop doors and that is to choose the winning door initially. There's a one in 3 chance of doing that. So the chances of winning if you swop are 2/3. If you wish to simulate the game, you only need to worry about the position of the car. Something like this.

C#
class Program
   {
       private static readonly Random random = new Random();
       static void Main()
       {
           int totalWins = 0;
           int iterations = 20000;
           for (int i = 0; i < iterations; i++)
           {
               totalWins += Play();
           }
           Console.WriteLine("% wins is {0:0.0%}", (double)totalWins / iterations);
           Console.ReadLine();

       }
       public static int Play()
       {
           int initialChoice = random.Next(3);//make a choice
           int winningDoor = random.Next(3);//the car is here
           //simulate the host choosing a door to open
           int openedDoor;
           do
           {
               openedDoor = random.Next(3);
           }
           //host chooses to open a door that's not the winning door or the door already picked
           while (openedDoor == initialChoice || openedDoor == winningDoor);
           //make the final choice the remaining door
           int finalChoice = 3 - (openedDoor + initialChoice);
           return finalChoice == winningDoor ? 1 : 0;
       }
   }

 
Share this answer
 
Comments
john1990_1 18-Apr-20 8:25am    
I would guess there's a more efficient way right?
George Swan 18-Apr-20 8:50am    
Yes

 public static int Play()
        {
            int initialChoice = random.Next(3);//make a choice
            int winningDoor = random.Next(3);//the car is here
            return initialChoice == winningDoor ? 0 : 1;
         }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900