自学内容网 自学内容网

c语言随机生成数字(用函数库)

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;
}


原文地址:https://blog.csdn.net/xinfanyyds/article/details/142897313

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!