Java Monty Hall游戏无法获得确切的概率

Java Monty Hall游戏无法获得确切的概率,java,math,probability,Java,Math,Probability,我对Monty Hall问题感到好奇,并尝试实施Monty Hall游戏,给出如下: 问题陈述如下。假设你在一个游戏节目中,你可以选择三扇门:一扇门后是一辆汽车;在其他人后面是山羊。你选了一扇门,比如说1号门,主人,谁知道门后面是什么,打开另一扇门,比如说3号门,门上有一只山羊。然后他对你说,“你想选2号门吗?”换一个选择对你有利吗 然而,我的成功率几乎是75%,而不是通常的66%。你能找到原因吗 //This is the results after 100 million iteration

我对Monty Hall问题感到好奇,并尝试实施Monty Hall游戏,给出如下:

问题陈述如下。假设你在一个游戏节目中,你可以选择三扇门:一扇门后是一辆汽车;在其他人后面是山羊。你选了一扇门,比如说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);

}
//这是经过1亿次迭代后的结果
//结果
//坚持选择
//0.2500521243
//改变选择
//0.7499478459
公务舱蒙蒂{
公共静态void main(字符串参数[]){
somethod();
}
公共静态方法(){
int总迭代次数=100000000;
int-trial=0;
int-win=0;
随机=新随机();
List initialDoorConfig=new ArrayList();
initialDoorConfig.add(1);
initialDoorConfig.add(0);
initialDoorConfig.add(0);
while(试用!=总迭代次数){
//确保随机性
Timestamp Timestamp=新的时间戳(System.currentTimeMillis());
random.setSeed(timestamp.getTime());
//创建随机门配置
Collections.shuffle(initialDoorConfig);
//游戏开始了
//玩家选择门
int playerChoiceDoorIndex=new Random().nextInt(initialDoorConfig.size());
//主人选择门
int hostChoiceDoorIndex=new Random().nextInt(initialDoorConfig.size());
/*
条件1:initialDoorConfig.get(hostChoiceDoorIndex)=1
原因:确保主人选择的门后面没有车。
条件2:hostChoiceDoorIndex==playerChoiceDoorIndex
原因:确保主办方的选择和球员的选择不一样
在满足这些条件后,我们可以肯定他们可以玩游戏。
*/
if(initialDoorConfig.get(hostChoiceDoorIndex)==1&&hostChoiceDoorIndex==playerChoiceDoorIndex){
//如果条件不满足,他们的游戏就不是我们感兴趣的正确游戏。
继续;
}否则{
//可以玩游戏并增加游戏索引
试用=试用+1;
//假设玩家总是待在他之前选择的门前
if(initialDoorConfig.get(playerChoiceDoorIndex)==1){
赢=赢+1;
}
}
}
System.out.println();
System.out.println(“坚持选择”);
System.out.printf(“%.10f”,(浮点)赢/总迭代次数);
System.out.println();
System.out.println(“-----------------------------------”);
System.out.println(“更改选择”);
System.out.printf(“%.10f”,((float)TOTAL_ITERATIONS-win)/TOTAL_ITERATIONS);
}

}

目前,代码中的主人随机选择一扇门,这与实际游戏不同,在实际游戏中,主人总是用山羊选择一扇门。我的数学不够好,无法解释为什么不计算无效游戏的概率会变为0.75

但如果您模拟主机的实际操作,您的程序将给出正确答案:

int hostChoiceDoorIndex=0;
对于(;hostChoiceDoorIndex<3;hostChoiceDoorIndex++){
如果(hostChoiceDoorIndex!=playerChoiceDoorIndex&&initialDoorConfig.get(hostChoiceDoorIndex)==0){
打破
}
}

“我的数学不够好,无法解释为什么不计算无效游戏的概率会变为0.75。”老实说,我为此奋斗了多年。我终于找到了一个简单的方法来向自己解释——写下每一个可能的奖励组合(1号门、2号门、3号门)和随后的选择(1号门-留一号门、1号门-选择2号门、1号门-选择2号门,等等),最后计算一下切换时你赢了多少次,以及留下来赢了多少次。这就是我最终掌握的方法。
//确保随机性
为什么你认为每次重新设置种子比使用种子更随机?pRNG被定制为使用单个种子生成足够随机的序列。当你改变它的时候,这是更随机的,因为这是一个简单的过程,你实际上是在颠覆定义良好的pRNG序列的数学建模算法。结果不一定是你会得到更好的随机分布——可能更糟。我的答案解决了这个问题吗?如果是这样,请考虑通过点击复选标记接受它!
int hostChoiceDoorIndex = 0;
for (; hostChoiceDoorIndex < 3 ; hostChoiceDoorIndex++) {
    if (hostChoiceDoorIndex != playerChoiceDoorIndex && initialDoorConfig.get(hostChoiceDoorIndex) == 0) {
        break;
    }
}