我只是想拥有简单的 RndInt(limit) 函数,该函数将返回以限制作为限制的随机数。
cout << "Enter higher limit of random range:" ;
cin >> limit;
while(limit != 0)
{
// RndInt(limit);
cout << "Random number smaller than " << limit << " is " << RndInt(limit) << endl;
cin.get();
cin.get();
cout << "Other random number smaller than " << limit << " is " << RndInt(limit) << endl;
cin.get();
cin.get();
cout << "There is " << RndInt(limit) << " ways I love you" <<endl ;
cout << "Enter higher limit of random range:" ;
cin >> limit;
}
return 0;
}
int RndInt(int limit)
{
srand(time(&base));
int rndnumber;
rndnumber=rand()%limit;
base+=100;
return rndnumber;
}
我在使 RndInt 后续调用显示不同的整数时遇到问题。当我调试其良好的后续调用 RndInt(limit) 时会给出不同的值。但是当我尝试在没有 cin.get() 暂停的情况下运行它时,我在所有三个调用中都得到相同的数字。我发现问题是种子在同一秒。
我的问题是如何使对 RndInt 的调用不间断地返回不同的值。 srand 函数该怎么办?
不要打电话srand()
每次你想要一个随机数时。称呼srand()
once在程序开始时,然后调用rand()
对于每个数字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)