Reputation: 31
I was curious about the Monty Hall problem and tried to implement the Monty Hall game given at:https://en.wikipedia.org/wiki/Monty_Hall_problem
Problem statement reads. 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?
However, my success percentage by switching the door come out to be almost 75% rather than the usual 66%. Can you find why?
//This is the results after 100 million iterations
//Result
//Staying with the choice
//0.2500521243
//Changing the choice
//0.7499478459
public class Monty {
public static void main(String args[]){
someMethod();
}
public static void someMethod() {
int TOTAL_ITERATIONS = 100000000;
int trial = 0;
int win = 0;
Random random = new Random();
List<Integer> initialDoorConfig = new ArrayList<>();
initialDoorConfig.add(1);
initialDoorConfig.add(0);
initialDoorConfig.add(0);
while(trial != TOTAL_ITERATIONS){
//Ensure Randomness
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
random.setSeed(timestamp.getTime());
//Create Random Door Configuration
Collections.shuffle(initialDoorConfig);
//Game Play Begins
//Player Chooses Door
int playerChoiceDoorIndex = new Random().nextInt(initialDoorConfig.size());
//Host Chooses Door
int hostChoiceDoorIndex = new Random().nextInt(initialDoorConfig.size());
/*
Condition 1: initialDoorConfig.get(hostChoiceDoorIndex) == 1
Reason: Makes sure the door chosen by host does not have a car behind it.
Condition 2: hostChoiceDoorIndex == playerChoiceDoorIndex
Reason: Makes sure hosts door choice and players door choice wasn't the same
Having met these conditions we can be sure they game can be played.
*/
if(initialDoorConfig.get(hostChoiceDoorIndex) == 1 && hostChoiceDoorIndex == playerChoiceDoorIndex){
//If the conditions are not met, they game is not a the right game we are interested in.
continue;
}else{
//Game can be played and increment the game index
trial = trial + 1;
//Assuming player will always stay with the door he choose before
if(initialDoorConfig.get(playerChoiceDoorIndex) == 1){
win = win + 1;
}
}
}
System.out.println();
System.out.println("Staying with the choice");
System.out.printf("%.10f", (float)win/TOTAL_ITERATIONS);
System.out.println();
System.out.println("------------------------------");
System.out.println("Changing the choice");
System.out.printf("%.10f", ((float)TOTAL_ITERATIONS - win)/TOTAL_ITERATIONS);
}
}
Upvotes: 2
Views: 160
Reputation: 273265
Currently, the host in your code chooses a door randomly, which is different from the actual game, where he always chooses a door with a goat. I am not good enough at maths to explain why not counting the invalid games changes the probability to 0.75.
But your program will give the correct answer if you simulate what the host actually does:
int hostChoiceDoorIndex = 0;
for (; hostChoiceDoorIndex < 3 ; hostChoiceDoorIndex++) {
if (hostChoiceDoorIndex != playerChoiceDoorIndex && initialDoorConfig.get(hostChoiceDoorIndex) == 0) {
break;
}
}
Upvotes: 2