我正在使用一个函数,该函数会生成2个随机整数,我稍后会使用它来请求用户添加并计算出总和。目前,我遇到了一个问题,它在重复相同的号码。我的随机数生成器正在打印重复数
这里的程序迄今:
#include<iostream>
#include<ctime>
using namespace std;
/*
This function generates a random integer
between 0 and max_int
*/
int get_rand_int(int max_int){
srand(time(0));
return int(rand()) % max_int;
}
This program generates random addition problems
with integers between 0 and 100
int main(){
int num1 = get_rand_int(101);
int num2 = get_rand_int(101);
cout << num1 << endl;
cout << num2 << endl;
请注意,这,这是一个家庭作业问题,我的老师布置。他给我们提供了骨架,我们试图填补空白。我有一种感觉,问题在于我的功能的srand部分,但这是我老师工作的一部分,所以我不确定是否需要触摸它。提前致谢。
*您知道*此之前已经问... –
@EricJ:虽然我同意,你所提供的链接是关于C#。更好的选择可能是http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once –
相同的确切问题。大多数语言事实上都有这个问题。随意投票结束作为你提供的一个副本。 –