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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。

声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。