【发布时间】:2018-10-14 09:42:52
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main( void){
int x = rand()%100;
printf("%d\n", x);
return 0;
}
上面的代码正确生成了一个随机数。它是否正确?但是,其他来源总是包括 library 和 srand(time(NULL))。为什么我们必须包含 include library 和 srand(time(NULL))?有什么理由加入吗?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void){
srand(time(NULL));
int x = rand()%100;
printf("%d\n", x);
return 0;
}
【问题讨论】:
-
在第一种情况下,每次运行程序时它总是相同的随机数。第二种情况“播种”了 RNG。