Monty Hall Java Program

Monty Hall Java Program | Manty Hall Problem is a game show where the audience is given the choice of three doors. Behind the two doors, goats are there, and behind another door, a car is there. Before starting the show, these goats and the car are placed randomly behind the doors.

The door which has a car is the prize door & you are supposed to choose the door having a car. Only in that case, you will win the game. Monty hall paradox game is a very famous teaching problem in statistics.

Monty Hall Paradox Game Rules

1) You are asked to choose a door. Before time begins, the door remains closed.

2) Monty Hall, the host of the game show knows what is there behind the door. Now he/she has to open one of the remaining doors. When the door opens, it must have a goat behind it.

3) If behind both remaining doors, a goat is there then the host chooses one door randomly.

4) After opening one door with a goat, again he gives you the second chance to reselect the door. You can decide to stay at the same door or switch to the last remaining door.

monty hall java program
Monty Hall Paradox Game

For example, if you have chosen door-1, then the host (Monty Hall) has to open either door-2 or door-3 & that door must have a goat. Image host has opened door-2. Now you have to decide whether you want to stay with your choice door-1 or want to switch to door-3. If your chosen door has a car then you win the game else you lose the game.

Monty Hall Java Program

import java.util.Random;
import java.util.Scanner;

public class MontyHall {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      Random generator = new Random();
      int userDoor, openDoor, otherDoor;

      // generate random value 1 to 3
      int prizeDoor = generator.nextInt(3) + 1;
      int goatDoor1 = prizeDoor;
      while (goatDoor1 == prizeDoor) {
         goatDoor1 = generator.nextInt(3) + 1;
      }
      int goatDoor2 = goatDoor1;
      while (goatDoor2 == goatDoor1 || goatDoor2 == prizeDoor) {
         goatDoor2 = generator.nextInt(3) + 1;
      }

      // begin Game
      System.out.println("Welcome to the Monty Hall game show!");
      System.out.print("Select the door (1, 2, or 3): ");
      userDoor = scan.nextInt();

      // validation
      if (userDoor > 3 || userDoor <= 0) {
         System.out.print("Please select door 1, 2, or 3");
         userDoor = scan.nextInt();
      } else {
         if(userDoor == goatDoor1) {
            openDoor = goatDoor2;
            otherDoor = prizeDoor;
         } else if(userDoor == goatDoor2) {
            openDoor = goatDoor1;
            otherDoor = prizeDoor;
         } else {
            openDoor = goatDoor1;
            otherDoor = goatDoor2;
         }

         System.out.println("\nIn a moment, I will show you " 
                 + " where the prize is located, but first I will"
               + " show you what is behind one of the other doors");
         System.out.println("\nBehind door number " + openDoor 
                        + " are goats!");
         System.out.println("You selected door number " + userDoor);
         System.out.print("\nWould you like to switch your"+
                         " door (y/n)? ");

         // take user input Yes or No
         char userReply = scan.next().charAt(0);
         while (userReply != 'y' && userReply != 'n') {
            System.out.print("Please enter either y/n");
            userReply = scan.next().charAt(0);
         }
         if (userReply == 'y') {
            userDoor = otherDoor;
            System.out.println("Your choice switched to door " 
                               + userDoor);
         }

         System.out.println("The prize is behind door number: " 
                           + prizeDoor);

         // check if user won or lost
         if (userDoor == prizeDoor) {
            System.out.println("Congratulations! You won the prize!");
         } else {
            System.out.println("Sorry. You lost the game.");
         }
         scan.close();
      }
   }
}

Output:-

Welcome to the Monty Hall game show!
Select the door (1, 2, or 3): 2

In a moment, I will show you where the prize is located, but first I will show you what is behind one of the other doors

Behind door number 1 are goats!
You selected door number 2

Would you like to switch your door (y/n)? n
The prize is behind door number: 2
Congratulations! You won the prize!

Welcome to the Monty Hall game show!
Select the door (1, 2, or 3): 3

In a moment, I will show you where the prize is located, but first I will show you what is behind one of the other doors

Behind door number 1 are goats!
You selected door number 3

Would you like to switch your door (y/n)? y
Your choice switched to door 2
The prize is behind door number: 3
Sorry. You lost the game.

So the task is to run the random simulation of the Monty Hall game. This game is to show how the strategy works for winning, Observe the output the chance of winning when you choose to change the door is much higher than when you don’t choose to change. Let us see the Monty Hall Java Program.

Program that simulates monty hall game Java

import java.util.Random;

public class MontyHall {
   public static void main(String[] args) {

      Random random = new Random();
      int trial = 1000000;

      System.out.println("The player never changes the door.");
      int win = 0;
      for (int i = 1; i <= trial; i++) {
         int prizeDoor = random.nextInt(3);
         int contestantDoor = random.nextInt(3);

         if (contestantDoor == prizeDoor) {
            win++;
         }
      }

      System.out.println("Total Wins out of " + trial + ": " + win);
      System.out.println("Percent win: " + (double) win / trial);
      System.out.println();

      System.out.println("The player always changes the door.");
      win = 0;
      for (int i = 1; i <= trial; i++) {
         int prizeDoor = random.nextInt(3);
         int contestantDoor = random.nextInt(3);

         int wrongDoor = prizeDoor;
         while (wrongDoor == prizeDoor || 
                wrongDoor == contestantDoor) {
            wrongDoor = random.nextInt(3);
         }

         int alternateDoor = 3 - (contestantDoor + wrongDoor);

         if (alternateDoor == prizeDoor) {
            win++;
         }
      }

      System.out.println("Total Wins out of " + trial + ": " + win);
      System.out.println("Percent win: " + (double) win / trial);
   }
}

Output:-

The player never changes the door.
Total Wins out of 1000000: 333488
Percent win: 0.333488

The player always changes the door.
Total Wins out of 1000000: 667534
Percent win: 0.667534

The player never changes the door.
Total Wins out of 1000000: 333105
Percent win: 0.333105

The player always changes the door.
Total Wins out of 1000000: 666258
Percent win: 0.666258

The player never changes the door.
Total Wins out of 1000000: 333506
Percent win: 0.333506

The player always changes the door.
Total Wins out of 1000000: 666795
Percent win: 0.666795

In the above Monty Hall Java Program, we have taken the Java Random class to generate random numbers. And we have taken the cases when the player never changes the door after his/her first choice and the case when the player always changes the door.

Out of 1000000 games, it shows the chance of winning percentage. The win percentage when the player changes the door is always greater than the case when the player doesn’t change the door. Also see:- Anagram Program In Java Using List

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.