【发布时间】:2019-02-07 07:02:38
【问题描述】:
我对蒙蒂霍尔问题很好奇,并尝试实现蒙蒂霍尔游戏: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);
}
}
【问题讨论】:
-
//Ensure Randomness为什么你认为每次重新设置种子比使用种子更随机更多?对 pRNG 进行定制以生成具有单个种子的足够随机序列。当您更改它时,从某种意义上说,这更加随机,因为您实际上是在颠覆定义明确的 pRNG 序列的数学建模算法。其结果不一定是你会得到一个更好的随机分布——可能会更糟。 -
我的回答是否修复了程序?如果是这样,请考虑通过单击该复选标记接受它!
标签: java math probability