Fundamentals 10 min read

Why Switching Doors Doubles Your Win Chance in the Monty Hall Problem – A Java Simulation

This article explains the classic Monty Hall problem, walks through the probability reasoning that switching doors raises the win chance from 1/3 to 2/3, and provides a complete Java program that simulates ten‑hundred‑thousand trials to verify the result.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Why Switching Doors Doubles Your Win Chance in the Monty Hall Problem – A Java Simulation

Problem Overview

The Monty Hall problem originates from the TV game show "Let's Make a Deal". A contestant faces three closed doors: behind one is a car, behind the other two are goats. After the contestant picks a door, the host, who knows what is behind each door, opens one of the remaining doors to reveal a goat and then offers the contestant the chance to switch to the other unopened door.

Probability Analysis

Three equally likely initial choices exist:

If the contestant initially picks a goat (probability 1/3 for each goat), the host must open the other goat door, leaving the car behind the remaining door. Switching wins the car.

If the contestant initially picks the car (probability 1/3), the host randomly opens one of the two goat doors (each with probability 1/2). Switching then leads to a goat.

Therefore, the probability of winning by switching is 1/3 + 1/3 = 2/3, while staying yields only 1/3.

Java Simulation

The following Java program reproduces the experiment by running 100 000 trials and counting how often switching results in a win.

public class ThreeDoors {
    private static final java.util.Random RANDOM = new java.util.Random();
    private static int SUCCESS_COUNT = 0;
    private static final int PLAY_TIMES = 100000;

    public static void main(String[] args) {
        for (int i = 0; i < PLAY_TIMES; i++) {
            playGame();
        }
        java.math.BigDecimal yield = new java.math.BigDecimal(SUCCESS_COUNT)
            .divide(new java.math.BigDecimal(PLAY_TIMES), 4, java.math.RoundingMode.HALF_UP)
            .multiply(new java.math.BigDecimal(100));
        System.out.println("Executed " + PLAY_TIMES + " trials, switch win rate: " + yield + "%");
    }

    public static void playGame() {
        boolean door1 = false, door2 = false, door3 = false;
        boolean pickedDoor;
        boolean leftDoor;
        // Place the car behind a random door
        switch (pickDoor(3)) {
            case 1: door1 = true; break;
            case 2: door2 = true; break;
            case 3: door3 = true; break;
        }
        int playerPickedDoor = pickDoor(3);
        // Host opens a goat door and we determine the remaining closed door
        if (playerPickedDoor == 1) {
            pickedDoor = door1;
            if (door2) leftDoor = door2;
            else if (door3) leftDoor = door3;
            else leftDoor = (pickDoor(2) == 1) ? door2 : door3;
        } else if (playerPickedDoor == 2) {
            pickedDoor = door2;
            if (door1) leftDoor = door1;
            else if (door3) leftDoor = door3;
            else leftDoor = (pickDoor(2) == 1) ? door1 : door3;
        } else {
            pickedDoor = door3;
            if (door1) leftDoor = door1;
            else if (door2) leftDoor = door2;
            else leftDoor = (pickDoor(2) == 1) ? door1 : door2;
        }
        // Switch to the remaining door
        pickedDoor = leftDoor;
        if (pickedDoor) SUCCESS_COUNT++;
    }

    public static int pickDoor(int bound) {
        return RANDOM.nextInt(bound) + 1;
    }
}

Running the program typically prints a win rate around 66‑67%, confirming the theoretical 2/3 probability for the switching strategy.

Conclusion

The Monty Hall puzzle illustrates how intuitive reasoning can be misleading; a systematic probabilistic analysis shows that always switching after the host reveals a goat doubles the chance of winning the car. Implementing the simulation in code provides concrete evidence and demonstrates how programmers can use experiments to verify mathematical claims.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

simulationstatisticsprobabilitymonty hall
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.