为何我用不均匀硬币产生出来的均匀硬币的比例是7:3左右，即使用了srand也只能到6：4，

0

```#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "windows.h"

int unbalancedcoin(){

int a = rand() % 10;
if (a > 6)return 0;
else return 1;
}
int banlancedcoin(){

SYSTEMTIME currentTime;
GetSystemTime(&currentTime);
srand(currentTime.wMilliseconds);

int a = unbalancedcoin();
int b = unbalancedcoin();
while (a == b){
b = unbalancedcoin();
}
return b;
}
int rand6(){
int a = ~(1 << 31);
while (a > 6){
a = 4 * banlancedcoin() + 2 * banlancedcoin() + banlancedcoin() - 6;
}
return a;
}
void main(){

int sum[6] = { 0 };
int tmp;
int a[10000];
/*for (int i = 0; i < 10000; i++){
tmp = rand6();
a[i] = tmp;
sum[tmp - 1]++;
}
for (int i = 0; i < 10000; i++){
printf("%d\t", a[i]);
}
printf("***********\n");
for (int i = 0; i < 6; i++){
printf("%d\t", sum[i]);
}*/

for (int i = 0; i < 10000; i++){

tmp = banlancedcoin();
a[i] = tmp;
sum[tmp]++;
}
for (int i = 0; i < 10000; i++){
printf("%d\t", a[i]);
}
printf("***********\n");
for (int i = 0; i < 2; i++){
printf("%d\t", sum[i]);
}

getchar();
}```

xqnq2007 | 初学一级 | 园豆：21

0

>6, 得到的要求是7 8 9 三个数字, 当然是7:3左右

-----

您需要登录以后才能回答，未注册用户请先注册