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.
会学会编程 ,首先必须会自己看明白语言参考手册