gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:01:03 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com