我正在做一个猜谜游戏的人工智能,我遇到了一个我自己似乎不能解决的问题。目标是让用户输入一个数字,让AI在合理的时间内猜测,我生成一个介于1-100之间的随机数字,并通过循环运行它来调整更低或更高。
void AI::AIguess(int usernum)
{
srand(time(NULL));
AIchoice = rand() % High + Low;
// "too high" or "too low" accordingly
do {
if (AIchoice == usernum)
{
cout << AIchoice << " is this correct?" << endl;
}
else if (AIchoice <= usernum)
{
cout << AIchoice << " seems a little low.." << endl;
Low = AIchoice;
AIchoice = 0;
AIchoice = rand() % High + Low;
AIguesses++;
}
else if (AIchoice >= usernum)
{
cout << AIchoice << " might have overshot a bit :/" << endl;
High = AIchoice;
AIchoice = 0;
AIchoice = rand() % High + Low;
AIguesses++;
}
} while (AIchoice != usernum);
}我使用前一个生成的数字作为下一个生成的数字的参数,希望能得到用户数量。它在if语句之间来回跳动,并分别调整高和低,但我面临的问题是,在循环几次之后,AIchoice开始添加超过100的值。有谁能帮我吗?
附言:非常感谢任何有帮助的AI创建信息:)
发布于 2018-12-18 05:19:36
区间代码中的随机数是错误的。要在min和max之间生成一个数字,请执行(rand() % (max - min)) + min。
因此,将AIchoice = rand() % High + Low;更改为AIChoice = (rand() % (High - Low)) + Low;。
https://stackoverflow.com/questions/53822960
复制相似问题