蒙蒂霍尔模拟中的意外结果

Unexpected results in Monty Hall simulation

本文关键字:意外 结果 模拟      更新时间:2023-10-16

根据我读到的概率,切换门应该产生~66%的机会选择正确的门。 下面的代码是我想出的,它吐出大约 50% 的胜利,而不是我预期的 66%。 任何关于我在这里出错的帮助将不胜感激。

for (int count = 0; count < 10000; count++)
{
    // Chooses which door contains DAT GRAND PRIZE YO.
    wDoor = rand() % 3 + 1;
    // AI Contestants Door choice
    aiDoor = rand() % 3 + 1;
    // Using oldChoice to ensure same door isn't picked.
    oldChoice = aiDoor;
    // Used in determining what door to open.
    openedDoor = aiDoor;
    // "Open" a door that is not the winning door and not the door chosen by player.
    do
    {
                openedDoor = rand() % 3 + 1;
    }while (openedDoor != wDoor && openedDoor != aiDoor);
    // Select new door between the remaining two.
    do
    {
              aiDoor = rand() % 3 + 1;
    }while (aiDoor != oldChoice && aiDoor != openedDoor);
    // Increment win counter if new door is correct.
    if (aiDoor == wDoor)
    {
               chooseAgain++;
    }
}

你的while条件是错误的:

while (openedDoor != wDoor && openedDoor != aiDoor)

应该是

while (openedDoor == wDoor || openedDoor == aiDoor)

等。

你的条件颠倒了。做...而 (...) 循环将按照您的注释描述执行,如果它们是重复的......直到(...),其端接测试的极性相反。

否定实现所需算法的条件。

请注意,在这两种情况下,您最多都有两扇门可供选择。利用这些知识,您最多只能使用 rand() 并且没有循环来确定隔壁。

// "Open" a door that is not the winning door and not the door chosen by player.
    do
    {
                openedDoor = rand() % 3 + 1;
    }while (openedDoor != wDoor && openedDoor != aiDoor);

当您打开获胜的门(!)或玩家选择的门时,此条件为假(即循环结束)。这与您想要的相反。

    // Select new door between the remaining two.
    do
    {
              aiDoor = rand() % 3 + 1;
    }while (aiDoor != oldChoice && aiDoor != openedDoor);

当玩家选择了与之前相同的门或打开的门时,这种情况是错误的(即循环结束)。这也与您想要的相反。

反转条件得到预期的结果 (~0.66)。