准备模拟一下10000次内两个数之差小于20的概率,但是输出结果是1和0,无法达到预期
#include <stdio.h>
#include <stdlib.h>
int main()
{
srand((unsigned)time(NULL));
int i = 0;
int count1 = 0;
int count2 = 0;
double percent1 = 0.0;
double percent2 = 0.0;
for (i = 1; i < 10000; i++)
{
long a = rand();
long b = rand();
if ((a - b) < 20 || (b - a) < 20)
{
count1++;
}
else
{
count2++;
}
}
percent1 = count1 / (count1 + count2);
percent2 = count2 / (count1 + count2);
printf("percent1=%lf,percent2=%lf\n", percent1, percent2);
return 0;
}
