C语言-随机数

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
", RAND_MAX, random_variable);
    return 0;
}
原文地址:https://www.cnblogs.com/asheng2016/p/7623261.html