我正在尝试制作一个使用6个骰子的骰子游戏。我可以每卷产生一个随机数,问题是我一直得到相同的数字(如果生成的数字是1,每个骰子是1)。是否有办法使每个骰子得到相同的数字?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main() {
int i;
int diceRoll;
srand(time(NULL));
int r = (rand()%6)+1;
printf("\t\t\t Welcome to Dice Game!\n");
for( i = 0; i < 6; i ++){
diceRoll= r;
printf(" %d \n", diceRoll);
}
return 0;
}
答案 0 :(得分:2)
将其放入for
循环:
r = (rand()%6)+1;
并在其之外声明r
:
int r;
OR
你不使用r,程序看起来像这样
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main() {
int count, diceRoll;
srand(time(NULL));
printf("\t\t\t Welcome to Dice Game!\n");
for(count = 0; count < 6; count ++){
diceRoll = (rand()%6)+1;
printf(" %d \n", diceRoll);
}
return 0;
}
答案 1 :(得分:1)
你有没有考虑过
for( i = 0; i < 6; i ++){
diceRoll= (rand()%6) + 1;
printf(" %d \n", diceRoll);
}