随机数

梦想与她 提交于 2019-11-27 05:50:39

定义和用法
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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!