c语言时间产生随机数代码解释 - 爱问答

(爱问答)

c语言时间产生随机数代码解释

#include "stdio.h"

#include "stdlib.h"

#include "time.h"

void main()

{   srand((unsigned int)time(NULL)); //srand()和time(NULL)的详细解释

  printf("%d ",rand());                     //rand()的解释

}

一.srand()

是随机种子生成器,它有一个参数,而不同的参数,会导致后面rand()随机函数的结果不同

完整的帮助(c语言参考手册)

Header File

stdlib.h 

Category

Math Routines

Prototype

int rand(void);

Description

Random number generator.

rand uses a multiplicative congruential random number generator with period 2 to the 32nd power to return successive pseudo-random numbers in the range from 0 to RAND_MAX. The symbolic constant RAND_MAX is defined in stdlib.h.

Return Value

rand returns the generated pseudo-random number.

二.time()

得到系统当前的时间,单位是秒,是1970年1月1日0时到目前的秒数

Header File

time.h 

Category

Time and Date Routines

Prototype

time_t time(time_t *timer);

Description

Gets time of day.

time gives the current time, in seconds, elapsed since 00:00:00 GMT, January 1, 1970, and stores that value in the location pointed to by timer, provided that timer is not a NULL pointer.

Return Value

time returns the elapsed time in seconds.

三rand()

产生从0到RAND_MAX范围内的随机整数,RAND_MAX由系统定义,在stdlib.h头文件中

Header File

stdlib.h 

Category

Math Routines

Prototype

int rand(void);

Description

Random number generator.

rand uses a multiplicative congruential random number generator with period 2 to the 32nd power to return successive pseudo-random numbers in the range from 0 to RAND_MAX. The symbolic constant RAND_MAX is defined in stdlib.h.

Return Value

rand returns the generated pseudo-random number.

会学会编程 ,首先必须会自己看明白语言参考手册

相关标签:c语言

下一篇:这是官方数据,统一的怎么会不一样

上一篇:请问如何定位光标的位置?在JTextPane中

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?