我正在使用一個函數,該函數會生成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 –
相同的確切問題。大多數語言事實上都有這個問題。隨意投票結束作爲你提供的一個副本。 –