我不明白为什么每轮游戏的x个游戏的输出结果是相同的掷硬币总数和相同的正面和反面数
int random_var(void);
int main(void)
{
int numHEAD = 0, numTAIL = 0, cointoss, numGAME, round = 1;
printf("Enter the number of games you wish to play: ");
scanf("%d", &numGAME);
srand((unsigned) time(NULL));
while (round <= numGAME)
{
while (numHEAD < 20 || numTAIL < 20)
{
cointoss = random_var();
printf("%d\n", cointoss);
if (cointoss == HEAD)
numHEAD++;
if (cointoss == TAIL)
numTAIL++;
}
if (numHEAD == 20)
printf("Game Tries/Game Event\n%-4d %d %d HEADs\n", round, numHEAD + numTAIL, numHEAD);
if (numTAIL == 20)
printf("Game Tries/Game Event\n%-4d %d %d TAILs\n", round, numHEAD + numTAIL, numTAIL);
round++;
}
return 0;
}
int random_var(void)
{
return rand() % 2;
}
最佳答案
你永远不会重置numHEAD
或numTAIL
所以一旦第一个游戏开始,它将为其他游戏重复该游戏。执行以下操作:
while (round <= numGAME) {
int numHEAD = 0, numTAIL = 0;
...
关于c - 在多个实例中多次生成0和1的随机值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40250790/