/*
Enter the number of sets: enter q to stop.
5 9
How many sides and how many dice:3
Here are 5 sets of 3 9-sided throws.
25 18 14 13 17
How many sets? Enter q to stop.
q
Press any key to continue
*/
#include
#include
#include
int diceroll(int num,int side); //返回骰子产生的数字的总和
int main(void) {
int sets,side,dice; //sets为回合数,side面,dice:骰子个数
int i; //变量初始化
srand((unsigned)time(NULL)); // 应在main()初始化随机数种子
printf("Enter the number of sets: enter q to stop.\n"); //set 组
while(scanf("%d",&sets) == 1 && sets > 0) {
printf("How many sides and how many dice:");
if(scanf("%d%d",&side,&dice) == 2) {
printf("Here are %d sets of %d %d-sided throws.\n",sets,dice,side);
for(i = 0;i < sets;i++) // 0-sets个回合
printf("%d ",diceroll(side,dice));//打印出返回值
printf("\n");
printf("How many sets? Enter q to stop. \n");
}
else printf("input side,and dice :\n");
}
return 0;
}
int diceroll(int side,int dice) {
int i,sum;
for(i = 0,sum = 0;i < dice;i++)
sum += rand()%side + 1; //产生3个骰子 摇出的总和sum
return sum; //返回3个骰子摇出的总数sum
}
看了你的程序,有点错误;
srand与rand函数前者必须设置发生随机数种子,如果没有则rand函不会根据种子发生随机数;
而你的srand(unsigned)没设置可以发生可变的随机数种子;
如srand(unsigned 1)就是一个产生可变随机种子数;
把srand 放到for循环的外面
你运行了几遍? 代码没问题,多试几次 还有问题请 截图(运行的图)