定义和用法
srand() 函数播种随机数生成器(rand())。
提示:从 PHP 4.2.0 开始,随机数生成器自动播种,因此没有必要使用该函数
srand(seed)。
seed可选
时间函数time():Return the time as seconds elapsed since midnight, January 1, 1970, or -1 in the case of an error.
time(NULL),因为时间每秒都在变化,因此srand()里面的参数也时刻在变化,可以达到随机的要求!!!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned int)time(NULL));
printf("%d\n",rand() %10); // 0-9
printf("%d\n",rand() %11 +10); // 10 - 19
printf("%d\n",rand());
printf("%d\n",rand());
printf("%d\n",rand());
/*printf("%d\n",time(NULL));*/
return 0;
}
来源:https://blog.csdn.net/weixin_44656803/article/details/99459759