1.使用 <stdlib.h> 和 <time.h> 库
2.使用 lib 库中的 rand() 函数 以及 srand() 函数
int rand() |
返回一个范围在 0 到 RAND_MAX 之间的伪随机数 |
void srand(unsigned int seed) |
该函数播种由函数 rand 使用的随机数发生器。 |
3.使用 time 库中的 time() 函数 根据不同的时间来随机生成不同的数字
time_t time(time_t *timer) |
计算当前日历时间,并把它编码成 time_t 格式。 |
最终具体实现代码如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>int main() {srand(time(0));int i;for(i = 1; i<= 5; i++) {printf("%d\n",rand());}return 0;
}