Java Implementation of the Monty Hall Puzzle

Java Implementation of the Monty Hall Puzzle

You are a contestant on a game show called "Let's Make a Deal" hosted by Monty Hall.

In front of you, there are three closed doors labeled 1, 2, and 3. Behind one of the doors is a car, and behind the other two are goats. You get to choose one door.

After you make your choice, Monty, who knows what's behind each door, opens one of the other two doors, revealing a goat. He then gives you the option to stick with your initial choice or switch to the other unopened door.

What should you do to maximize your chances of winning the car?

Options:

  1. Stick with your initial choice.
  2. Switch to the other unopened door.
  3. It doesn't matter; your chances are the same either way.

If you chose option (1) Stick with your initial choice, that's a common intuition. However, the correct answer is actually option (2) Switch to the other unopened door.

By switching doors, you double your chances of winning the car from 1/3 to 2/3. This might seem counterintuitive at first, but it's a fascinating aspect of probability known as the Monty Hall problem.


Explanation :

The counterintuitive result of the Monty Hall problem arises from the conditional probabilities involved in the game. Let's break down why switching doors increases the chances of winning the car:

  1. Initial Choice: When the contestant initially selects one of the three doors, there's a 1/3 probability that they've chosen the door with the car behind it. Conversely, there's a 2/3 probability that the car is behind one of the other two doors.
  2. Revelation by the Host: When the host, who knows what's behind each door, opens one of the other two doors to reveal a goat, this doesn't change the initial probabilities. However, it provides valuable information: it reveals one of the doors that definitely doesn't contain the car.
  3. Switching Doors: By switching doors after the host reveals a goat, the contestant effectively bets that their initial choice was wrong. Since there's a 2/3 chance that the car is behind one of the unchosen doors, switching increases the likelihood of winning to 2/3.

To understand this conceptually, consider a hypothetical scenario with 100 doors instead of just 3. Initially, the probability of choosing the door with the car is 1/100, while the probability of the car being behind one of the other 99 doors is 99/100. When the host opens 98 doors to reveal goats, the probability that the car is behind one of the remaining unopened doors (other than the contestant's initial choice) is still 99/100. Therefore, switching doors in this scenario is clearly advantageous.

In summary, the counterintuitive result occurs because the information revealed by the host changes the probabilities in favor of switching doors, even though it may seem initially that the contestant's odds should remain the same.


Implementation using Java :

playGame function simulates a single game of the Monty Hall problem. It randomly selects a door to hide the car behind and the contestant's initial choice. If the contestant decides to switch doors, it updates the chosen door accordingly. Finally, it returns whether the contestant has won the car.

This function encapsulates the game logic of the Monty Hall problem, including door selection and potential switching, and is used to simulate multiple games in the main loop.

private static boolean playGame(boolean switchDoor) {

    // Randomly decide the position of the car
    int carPosition = random.nextInt(3);
    // Randomly choose a door
    int chosenDoor = random.nextInt(3);

    // If the strategy is to switch doors, switch the chosen door
    if (switchDoor)
        chosenDoor = switchDoor(chosenDoor, carPosition);

    // Return true if the chosen door has the car, false otherwise
    return chosenDoor == carPosition;
}        

The switchDoor function simulates the contestant switching doors in the Monty Hall problem. Given the contestant's initial choice (chosenDoor) and the door behind which the car is hidden (carPosition), it randomly selects a door to reveal that is neither the contestant's choice nor the door with the car behind it. Finally, it returns the index of the other unopened door that the contestant can switch to.

This function ensures that the revealed door is not the contestant's initial choice or the door with the car behind it, as required by the rules of the Monty Hall problem. It facilitates the switching of doors by the contestant, ultimately influencing the outcome of the game.

private static int switchDoor(int chosenDoor, int carPosition) {

   // Randomly reveal a door
   int revealedDoor = random.nextInt(3);

   // reveal a door which is not the player choice / the car door
   while (revealedDoor == chosenDoor || revealedDoor == carPosition)
      revealedDoor = random.nextInt(3);

   // Return the other unopened door
   return 3 - chosenDoor - revealedDoor;
}        

The main function runs a simulation of the Monty Hall problem by playing a specified number of games (totalGames). For each game, it tracks the number of wins by staying with the initial choice (stayWins) and the number of wins by switching doors (switchWins).

After simulating all games, it calculates and prints the win percentages for both strategies.

This function serves as the entry point for the simulation and summarizes the game outcomes succinctly.

public static void main(String[] args) {

    int totalGames = 200000; // Total number of games to be played
    int stayWins = 0; // Number of games won by staying
    int switchWins = 0; // Number of games won by switching
    
    // Play the game totalGames times
    for (int i = 0; i < totalGames; i++) {

         // Play the game with stay strategy
         if (playGame(false))
              stayWins++;

        // Play the game with switch strategy
        if (playGame(true))
             switchWins++;
    }

    // Calculate and print the win percentage for switch strategy
    double stayWinPercentage = ((double) stayWins / totalGames) * 100;
    double switchWinPercentage = ((double) switchWins / totalGames) * 100;

    System.out.println("Win percentage if stay: " + stayWinPercentage);
    System.out.println("Win percentage if switch: " +switchWinPercentage);
}        

console output result :

Win percentage if stay: 33.3799995%

Win percentage if switch: 66.6675%        

These results align with the theoretical expectations of the Monty Hall problem, demonstrating that switching doors indeed provides a higher chance of winning compared to sticking with the initial choice.

Thank you for joining me on this exploration of Monty Hall problem. What are your takeaways from our discussion? Feel free to share your insights and reflections in the comments.


Asala Zreik

Experienced Software Engineer | Java Developer | Amdocs

1 年

Nice work Lawrence ???? Insightful and well written !

Itay Adi Yosef

Front-End Web Developer | ReactJS | TypeScript | Redux | Expert Problem Solver

1 年

But if I want a goat?

要查看或添加评论,请登录

Lawrence Ashkar的更多文章

社区洞察

其他会员也浏览了