C语言-随机数

时间:2023-01-30 17:20:54

C语言使用rand()函数产生随机数, 使用rand()函数之前要先使用srand(time(0)),

以当前时间作为种子, 否则产生的随机数将不会变化.

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int main () {
srand(time(0)); //use current time as seed for random generator
int random_variable = rand();
printf("Random value on [0, %d]: %d\n", RAND_MAX, random_variable);
return 0;
}