srandom and random

今天看了看这两个函数的manpage,srandom 的作用是为random 做个种子,默认random生成的数是以srandom(1)为种子的。所以,虽然random 生成的数好像没什么规律,但每次生成的数都是一样的。如果想要改变这个所谓的“随机数”,就要再说srandom生成一个新的种子,比如 srandom(2),不过这样做太麻烦了,先拿srandom(time(0))充个数吧。

#include <stdlib.h>
#include <stdio.h>

int main()
{
     srandom(time(NULL));
     printf(“the number is %ld\n”, random());
}

The random function use a seed to generate a random long int, and the srandom function can change the seed that the random function use.

Then the srandom function is short for a setup_random_function.

原文地址:https://www.cnblogs.com/henyihanwobushi/p/2690289.html