C语言生成随机数和随机数组实现方式
作者:这个饕字怎么读
文章主要介绍了在C语言中生成随机数和随机数组的方法,需要包含<time.h>和<stdlib.h>头文件,使用time()和rand()函数实现,最后强调了经验分享和个人支持
C语言生成随机数和随机数组
- time函数要包含头文件<time.h>
- rand函数要包含头文件<stdlib.h>
随机数
int a; srand((unsigned)time(NULL)); // 播种 a = rand() % 200 + 1;
随机数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 生成元素为1-200的随机数组
int main()
{
int a[100],aa;
int i;
int count = 0;
// 按时间重新播种
srand((unsigned)time(NULL));
for (i = 0; i < 100; i++)
{
aa = rand() % 200 + 1;
a[i] = aa;
}
for (i = 0; i < 100; i++)
{
printf("%-5d", a[i]);
count++;
if (count == 10)
{
printf("\n");
count = 0;
}
}
system("pause");
return 0;
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
