蒙蒂·霍尔游戏无法获得确切的概率

时间:2019-02-07 07:02:38

标签: java math probability

我对Monty Hall问题感到好奇,并尝试实施在以下地方给出的Monty Hall游戏:https://en.wikipedia.org/wiki/Monty_Hall_problem

问题语句读取。假设您正在一场游戏节目中,并且可以选择三扇门:一扇门后面是一辆汽车;在其他人之后,是山羊。您选择一扇门,说一号,然后知道门后有什么的主人打开另一扇门,说三号,里面有一只山羊。然后,他对您说:“您要选择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 :(得分:2)

当前,代码中的主机随机选择一扇门,这与实际游戏不同,在实际游戏中,他总是选择带有山羊皮的门。我没有足够的数学知识来解释为什么不计算无效游戏会导致概率变为0.75。

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

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