Wikipedia: Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?

Vos Savant’s response was that the contestant should switch to the other door (vos Savant 1990a). Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their initial choice have only a 1/3 chance.

Using Java, I tested this problem for 10000 times and I found that wins percentage is 33% when player does not change their decision after host opened one of door without car. If player changes decision, wins percentage is 66% which is double comparing to if player does not change.

Code:

import java.util.Arrays;

import java.util.Random;

public class MontiHall {

// used to generate random numbers

static Random r = new Random();

static int doorsAmount = 3; //3 doors

static int tries = 10000; //how many times tested

static boolean doesPlayerChangedChoice = true; //whether player changes decision after host opened one of door without car

static int wins = 0;//amount to save win times

public static void main(String[] args) {

for (int k = 0; k < tries; k++) {

playGame();

}

print("wins:"+wins*100/tries+"%");// print win percentage

}

private static void playGame() {

// generate array of doors. if 0 - no car, if 1 - with car

ArrayList<Integer> doors = new ArrayList<>();

int doorWithCar = r.nextInt(doorsAmount);

for (int i = 0; i < doorsAmount; i++) {

if(i==doorWithCar) doors.add(1);

else doors.add(0);

}

print(Arrays.toString(doors.toArray()));// eg. [0, 1, 0]

int doorSelectedByPlayer = r.nextInt(doorsAmount);//door index which is selected by player

int doorSelectedByHost;

while(true){

doorSelectedByHost = r.nextInt(doorsAmount);

// door selected by host should not be same with player selected and should not have car

if(doorSelectedByPlayer!=doorSelectedByHost && doorSelectedByHost!=doorWithCar) break;

}

// print selected doors indices

print("player:"+doorSelectedByPlayer);

print("host:"+doorSelectedByHost);

// if player changes decision

if(doesPlayerChangedChoice){

while(true){// find new door which is not same to previous selected door and not same with host selected

int anotherDoorSelectedByPlayer = r.nextInt(doorsAmount);

if(anotherDoorSelectedByPlayer!=doorSelectedByHost && anotherDoorSelectedByPlayer!=doorSelectedByPlayer){

doorSelectedByPlayer = anotherDoorSelectedByPlayer;

print("player changes:"+doorSelectedByHost);

break;

}

}

}

if(doorSelectedByPlayer==doorWithCar){//win

wins++;

print("win! car:"+doorWithCar+" player:"+doorSelectedByPlayer);

}

else print("lose! car:"+doorWithCar+" player:"+doorSelectedByPlayer);//lose

print("");

}

private static void print(String message){

System.out.println(message);

}

}

Output:boolean doesPlayerChangedChoice = false;

**wins:33%**

Output:boolean doesPlayerChangedChoice = true;

**wins:66%**