Мне было любопытно узнать о проблеме Монти-Холла и попытаться реализовать игру Монти-Холла, заданную по адресу: https://en.wikipedia.org/wiki/Monty_Hall_problem
.Предположим, вы участвуете в игровом шоу, и у вас есть выбор из трех дверей: за одной дверью находится машина;позади остальных коз.Вы выбираете дверь, скажем, № 1, и хозяин, который знает, что за дверями, открывает другую дверь, скажем, № 3, у которой есть коза.Затем он говорит вам: «Вы хотите выбрать дверь № 2?»Вам выгодно менять свой выбор?
Тем не менее, мой процент успеха при переключении двери оказался почти 75%, а не 66%.Вы можете найти почему?
//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);
}
}