in1*_*t3r -2 c++ random time srand
我只是试图简单地使用RndInt(限制)函数来返回限制为极限的随机数.
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;
}
Run Code Online (Sandbox Code Playgroud)
我有问题让RndInt后续调用显示不同的整数.当我调试其精细的后续调用RndInt(限制)给出不同的值.但是当我尝试在没有cin.get()暂停的情况下运行它时,我在所有三个调用中得到相同的数字.我发现问题在于种子是在同一秒.
我的问题是如何调用RndInt返回不同的值而不会暂停.怎么做srand功能?
| 归档时间: |
|
| 查看次数: |
2343 次 |
| 最近记录: |