对于以下代码:
#include<stdlib.h>
#include<stdio.h>
int main()
{
int x;
x = rand()%100;
printf("The Random Number is: %i", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
似乎总是将随机数打印为83.这是为什么?
大多数随机数生成器都是可重复的.您需要在使用之前为生成器播种,通常使用系统时间.
#include <time.h>
srand(time(NULL));
Run Code Online (Sandbox Code Playgroud)