当调用函数时,需要关心5要素:
- 头文件:包含指定的头文件
- 函数名字:函数名字必须和头文件声明的名字一样
- 功能:需要知道此函数能干嘛后才调用
- 参数:参数类型要匹配
- 返回值:根据需要接收返回值
#include <time.h>time_t time(time_t *t);功能:获取当前系统时间参数:常设置为NULL返回值:当前系统时间, time_t 相当于long类型,单位为毫秒#include <stdlib.h>void srand(unsigned int seed);功能:用来设置rand()产生随机数时的随机种子参数:如果每次seed相等,rand()产生随机数相等返回值:无#include <stdlib.h>int rand(void);功能:返回一个随机数值参数:无返回值:随机数
// 产生随机数#include <stdio.h>#include <time.h>#include <stdlib.h>int main(){time_t tm = time(NULL);//得到系统时间srand((unsigned int)tm);//随机种子只需要设置一次即可int r = rand();printf("r = %d\n", r);return 0;}
