rand()函数在C++中无法正常工作
rand() function does not work properly in C++
我正在编写一个游戏,其中计算机选择一个用户必须猜测的随机数。然后,用户选择一个数字(他只是猜测一个数字),计算机必须猜测。问题是,我在游戏的第二部分使用的rand()函数有时会提供一个新范围之外的数字。例如,如果新的范围是低=4,高=10,rand()提供12,这是不正确的。我试图理解为什么会出现错误,但我找不到。我正在Dev-C++中写作。代码就在下面,后面跟着输出。谢谢你的帮助和时间。
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int computer_guess, nbr, nbrguesses, count_user_guess=0, count_computer_guess=0;
char user_comparison;
int low=1, high=99; //initialize low to the lower boundary and high to the higher boundary
srand(time(0));
nbr=rand()% 99 + 1; //computer chooses a random number between 1 and 99...
do
{
cout << "guess a number now! " << endl;
cin >> nbrguesses; //user enters the number he guessed...
if ( nbrguesses>nbr)
cout << "too big" <<endl;
else if ( nbrguesses<nbr)
cout << "too small" << endl;
else if ( nbrguesses==nbr)
cout << " you found the correct number! " <<endl;
count_user_guess++; //count the number of guesses from the user
}
while (nbrguesses!=nbr);
cout << "you tried " << count_user_guess <<" times to find the right number " << endl<<endl;
cout << "----------------------------------------------------------------------" << endl;
cout << "Now, the computer will guess your number " << endl;
do
{
srand(time(0));
computer_guess=rand()% high + low; //computer guesses the number
cout << "computer guess: " << computer_guess <<endl;
cout << "is it the correct number?" << endl;
cin >> user_comparison; //if user enter
if (user_comparison=='>') // character '>' it means the number guessed by computer is too big
{
cout << "too big" <<endl;
high= computer_guess-1; //high is given a new boundary
cout << "***Current value of high = " << high << " and low = " << low << endl; //display current boundaries
}
else if ( user_comparison=='<') //the number guessed by computer is too small
{
cout << "too small" << endl;
low= computer_guess+1; //low is given a new boundary
cout << "***Current value of low = " << low << " and high = " << high << endl; //display current boundaries
}
else if ( user_comparison=='=')
cout << "Computer found the correct number! " <<endl;
count_computer_guess++; //count number of guesses from computer
}
while (user_comparison!='=');
cout << "The computer tried " << count_computer_guess <<" times to find the right number " << endl;
cout << "The Game is over now, have a good day!" << endl;
getch();
return 0;
}
//**************Output******************************************
guess a number now!
50
too big
guess a number now!
25
you found the correct number!
you tried 2 times to find the right number
----------------------------------------------------------------------
Now, the computer will guess your number
computer guess: 11
is it the correct number?
>
too big
***Current value of high = 10 and low = 1
computer guess: 3
is it the correct number?
<
too small
***Current value of low = 4 and high = 10
computer guess: 12
is it the correct number?
您必须更换
computer_guess=rand()% high + low;
通过
computer_guess=rand()%(high- low + 1) + low;
在你的例子中,你的低=4,高=10。但是你想要一个0到6之间的随机数,再加上4。0到10之间没有一个,从那时起,你可以得到14个结果。如果你不想包括上限(Interval[low,high]而不是[low,height]),你必须省略括号中的+1。
为什么
rand()返回一个相当大的整数。我们想要得到区间[a,b]中的一个随机数。如果我们取rand()%5,我们得到一个0,1,2,3或4的数字,因此从区间[0,4]。在一般情况下,rand()%C在区间[0,C-1]中给出一个随机数。
如果我们加上一个常数值,例如Rand()%C+D,则间隔被移位:[D,C-1+D]。回到问题上来,我们希望区间是[a,b]。因此,我们需要a=D作为下键,C-1+D=b作为上键。我们可以将其转换为C=b-D+1=b-a+1。所以我们想要使用rand()%(b-a+1)+a。
希望这能解释一下事情是如何运作的。
你很接近,但试试这个:
rand()%(high-low)+low;
srand(time(0));
computer_guess=rand()% high + low; //computer guesses the number
首先,不要在循环中调用srand
。程序启动时只需调用一次即可。
其次,这种逻辑是错误的。应该是:
computer_guess=low + (rand() % (high-low+1));
(这假设高和低是包含的。也就是说,如果高是10,低是1,那么1和10都是可以接受的猜测。)
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程