Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# copeg

#### The Monty Hall Problem

###### Rate this Entry
by , May 16th, 2011 at 07:29 PM (24892 Views)
The Monty Hall problem is a statistical problem which originates from the television Game show Lets Make a Deal, hosted by Monty Hall. The game is simple: a contestant is presented 3 closed doors, behind one of which is a valuable prize (oftentimes described as a car, whereas the other doors have goats behind them). A contestant chooses a door. The host then opens one of the doors you did not choose, which does NOT contain the prize. Then the host asks - do you want to change your decision? What is the probability you will win if you choose to change doors? What is the probability you win if you choose to remain with your decision.

Simplistically, one might guess your probability of winning went from 1/3, to 1/2. This however, is incorrect. If you stay, your probability of winning is still 1/3. If you change, your chances of winning is 2/3...How does this counter-intuitive result play out? One can tabulate all the possibilities, and the contestants decision:

```Door1		Door 2		Door 3		Switch		Stay
Prize		Nothing		Nothing		Prize		Nothing
Nothing		Prize		Nothing		Nothing		Prize
Nothing		Nothing		Prize		Nothing		Prize```
Looking at the chart carefully*: if you switch your chances go from 1/3 to 2/3 of winning. Still not convinced? Below is a simple simulation of this problem. This iterates through a game if n number of doors a defined number of times, tallying up whether you win or loose based upon staying
or changing...and the simulation backs everything up - average for 3 doors: stay: 33%, change 66%.

```
import java.util.Random;

/**
* Simulates the MontyHall problem. 3 doors, 2 with goats and 1 with car. You choose a door,
* Monty hall opens one of the other two to reveal a goat. How often will you be correct if you
* stay? How often if you switch?
* @author copeg
*
*/
public class MontyHallSimulation implements Runnable{

/*Random number generator */
private static final Random RANDOM = new Random();
/*Number of rounds to simulate*/
private int rounds;
/*Number of doors total*/
private int doors;
/*Rate for staying*/
private double stayRate = 0;
/*Rate for changing*/
private double changeRate = 0;

/**
* Constructs a MontyHallSimulation with 3 doors, to iterate 1000 times.
*/
public MontyHallSimulation(){
this(1000);
}
/**
* Constructs a MontyHallSimulation with the number of rounds to simulate, and
* 3 doors.
* @param rounds The number of rounds to simulate.
*/
public MontyHallSimulation(int rounds){
this(rounds, 3);
}

/**
* Constructs a MontyHallSimulation with the number of rounds and doors to use in
* the simulation.
* @param rounds The number of rounds to simulate
* @param doors The number of doors to use in the simulation.
* @throws IllegalArgumentException if doors is less than 3.
*/
public MontyHallSimulation(int rounds, int doors){
if ( doors < 3 ){
throw new IllegalArgumentException("Cannot simulate the problem with less than 3 doors.");
}
this.rounds = rounds;
this.doors = doors;
}

/**
* Implementation of the Runnable interface. Simulates the Monty Hall Problem.
* This loops doors number of times, determining whether staying or changing
* results in a correct answer.
*/
public void run(){
int stayCount = 0;
int changeCount = 0;
for ( int i = 0; i < rounds; i++ ){
int choose = RANDOM.nextInt(doors);//choose a door at random
int solution = RANDOM.nextInt(doors);//find a random place where the car will be.
if ( solution != choose ){//Car is in the other door - if you change you win
changeCount++;
}else{//If you stay you win.
stayCount++;
}
}
stayRate = stayCount/(double)rounds;
changeRate = changeCount/(double)rounds;
}

/**
* Retrieves the rate one will be correct if one stays. This method returns
* zero unless run has been called.
* @return
*/
public double getStayRate(){
return stayRate;
}

/**
* Retrieves the rate one will be correct if one changes.  This method returns
* zero unless run has been called.
* @return
*/
public double getChangeRate(){
return changeRate;
}

/**
* Application entry point.
* @param args
*/
public static void main(String[] args){
MontyHallSimulation sim = new MontyHallSimulation(1000, 1000);
sim.run();
System.out.println("Choose to stay: percent correct - " + sim.getStayRate());
System.out.println("Choose to change: percent corect - " + sim.getChangeRate());
}

}```

How about the same problem with 1000 doors? You are virtually guaranteed to win if you change your decision (what are the chances you chose the right door in the first place?).

A fun statistical problem to investigate and simulate...happy coding!

*The chart does not translate well in this format, however the link below contains a better, more readable version to inspect.