编写一个产生1000个1到10范围内的随机整数,统计各数字出现的次数
#include<stdio.h>
#include<stdlib.h>//为rand,srand函数提供原型;#define NUM 1000
int main(void)
{
unsigned int seed;
int i,l;
int a[10]={0,0,0,0,0,0,0,0,0,0};
puts("enter a unsigned positive integer: ");
while(scanf("%u",&seed)==1)/* 循环输入不同的程序值;*/
{
printf("you enter the seed is %d\n",seed);
srand(seed); /*改变系统提供的种子值*/
for(i=0;i<NUM;i++)
{
switch((rand()%10+1))//累加各个数字出现的次数;//
{
case 1:a[0]+=1;break;
case 2:a[1]+=1;break;
case 3:a[2]+=1;break;
case 4:a[3]+=1;break;
case 5:a[4]+=1;break;
case 6:a[5]+=1;break;
case 7:a[6]+=1;break;
case 8:a[7]+=1;break;
case 9:a[8]+=1;break;
case 10:a[9]+=1;break;
default:break;}
}
for(l=1;l<11;l++) /*打印结果*/'
printf(" 数字%3d 出现%4d 次\n",l,a[l-1]);
}
}