c++ - Monty Hall 模拟中的意外结果

标签 c++

根据我读到的概率,切换门应该有大约 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)

等等

关于c++ - Monty Hall 模拟中的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553209/

相关文章:

c++ - OpenCV 和虚拟 C++ : save Frame data from Camera and then save to Jpg

c++ - 为 std 库类定义运算符

c++ - 获取 2 个 QDir 的共同父级

c++ - 静态链接 g++ 时出错

c++ - 强制派生类覆盖数据成员

c++ - 在 MFC 应用程序内部使用时 TBB 内存泄漏

c++ - 为什么删除移动构造函数时我的对象没有被复制?

c++ - C++ 中是否有创建 .bin 文件的函数,或者此代码是否缺少某些内容?

c++ - CLion 添加依赖 header 和源

c++ - 可变参数模板和复制构造函数