#include<stdio.h>
#include<time.h>
#define sum 100
int main()
{
int i=0;
int temp=0;
int select_first=0,select_second=0,select_third=0;
srand((unsigned int)time(NULL));
for(i=0;i<sum;i++)
{
temp=rand()%100+1;
if(temp<=30)
select_first++;
else if(temp>30 && temp<=80)
select_second++;
else
select_third++;
}
printf("select_first/sum=%f应该约等于30%/n",(float)select_first/(float)sum);
printf("select_second/sum=%f应该约等于50%/n",(float)select_second/(float)sum);
printf("select_third/sum=%f应该约等于20%/n",(float)select_third/(float)sum);
}
输出结果:
select_first/sum=0.300000应该约等于30%
select_second/sum=0.520000应该约等于50%
select_third/sum=0.180000应该约等于20%