Skip to main content

Java - Monty Hall Problem

This problem is based on a television show where a contender is asked to select one of the 3 doors. Behind one door, there is a car and behind others there is a goat.
The host ask the user to select one door, when he/she did , the host asks if the user wants to switch the door. And afterwards, the doors are revealed. So, this problem focuses on whether the probability of winning is higher when user switches the door. 
It was found out that if you switch , probability of winning is 2/3 as opposed to 1/3 if you don't.
Read more about this here Monty Hall Problem
In this article, we will make a program to find out the probability of winning if user switches the door and if he/she does not.
import java.util.Random;
import java.util.Scanner;
public class Monty {
        
        private static Scanner scan ;
        private static Random rand;
        public static void main(String[] args) {
                
                scan = new Scanner(System.in);
                rand = new Random();
                
                System.out.println("MONTY HALL PROBLEM\n");
                
                // Ask user to enter N
                System.out.print("Enter number of rounds : ");
                int N = Integer.parseInt(scan.nextLine());
                int totalWins = 0;
                
                // Switching gates
                // Iterate game N times
                for(int i = 1;i<=N;i++) { 
                        // Generate a random winning door
                        int winningDoor = rand.nextInt(3)+1;
                        
                        // Generate a random door chosen by user
                        int chosenDoor = rand.nextInt(3)+1;
                        
                        // Generate the other door
                        // apart from winning door and chosenDoor
                        int otherDoor = getAnotherDoor(winningDoor, chosenDoor);
                        
                        // Switch door
                        // Means generate another chosen door for user
                        // apart from otherDoor and itself (chosenDoor)
                        int switchedDoor =  getAnotherDoor(otherDoor, chosenDoor);
                        
                        // Check if switched door and winning door are same
                        // If yes increment totalWins by 1
                        if(switchedDoor == winningDoor) {
                                totalWins+=1;
                        }       
                }            
                // Print total wins for switching gates
                System.out.println("\nTotal wins by switching door: "+totalWins);
                double chances = ((double)totalWins/N)*100;
                System.out.println("Chances of winning by switching door: "+ chances+"%");
                
                // Re-set totalWins to 0
                totalWins = 0;
                
                // Not switching gates
                // Iterate game N times
                for(int i = 1;i<=N;i++) {                        
                        // Generate a random winning door
                        int winningDoor = rand.nextInt(3)+1;
                        
                        // Generate a random door chosen by user
                        int chosenDoor = rand.nextInt(3)+1;

                        // Check if chosen door and winning door are same
                        // If yes increment totalWins by 1
                        if(chosenDoor == winningDoor) {
                                totalWins+=1;
                        }
                }                
                // Print total wins for not switching gates
                System.out.println("\nTotal wins by not switching door: "+totalWins);
                chances = ((double)totalWins/N)*100;
                System.out.println("Chances of winning by not switching door: "+ chances+"%");
        }
        
        
        private static int getAnotherDoor(int door1, int door2) {
                
                // Generate random number between 1-3 and set to door3
                // If value generate is equal to door1 or door2
                // Generate value for door3 again
                // Keep doing this until a value apart from door1 and door2 is generated
                int door3 = rand.nextInt(3)+1;
                while(door3 == door1 || door3 == door2) {
                        door3 = rand.nextInt(3)+1;
                }
                return door3;
        }
}
OUTPUT-
 



Comments

Popular Posts