【问题标题】:Monty Hall Game Unable to get an exact probability蒙蒂霍尔游戏无法得到准确的概率
【发布时间】: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


【解决方案1】:

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

但如果你模拟主机实际所做的事情,你的程序会给出正确答案:

int hostChoiceDoorIndex = 0;
for (; hostChoiceDoorIndex < 3 ; hostChoiceDoorIndex++) {
    if (hostChoiceDoorIndex != playerChoiceDoorIndex && initialDoorConfig.get(hostChoiceDoorIndex) == 0) {
        break;
    }
}

【讨论】:

  • 我的数学还不够好,无法解释为什么不计算无效游戏会将概率变为 0.75。”老实说,我为这个问题苦苦挣扎了多年。我终于找到了一种简单的方法来向自己解释——写下奖励所在位置(门 1、2、3)和随后的选择(门 1-留一个,门 1-选择 2,门 1)的所有可能组合-选择2等),最后计算你在切换时赢了多少次,而你留下来赢了多少次。这就是我最终掌握它的方式。
猜你喜欢
  • 1970-01-01
  • 2018-02-13
  • 2012-10-18
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2010-11-17
  • 2020-07-14
相关资源
最近更新 更多