我正在制作一个模拟骰子滚动100次的程序,并跟踪每个数字的滚动次数。问题是数字生成器一直在反复输出相同的数字。它将它们加在一起很好,但它只是一个输出的数字。这是代码:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int Diceroll (int);
int main() {
int num = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0;
int i = 0, j = 100;
for(int i = 0; i < j; i++) {
num = 0;
num = Diceroll(num);
cout << num << endl;
if(num == 1) {
sum1 += 1;
}
else if(num == 2) {
sum2 += 1;
}
else if(num == 3) {
sum3++;
}
else if(num == 4) {
sum4++;
}
else if(num == 5) {
sum5++;
}
else if(num == 6) {
sum6++;
}
}
cout << "1 was rolled " << sum1 << " times." << endl;
cout << "2 was rolled " << sum2 << " times." << endl;
cout << "3 was rolled " << sum3 << " times." << endl;
cout << "4 was rolled " << sum4 << " times." << endl;
cout << "5 was rolled " << sum5 << " times." << endl;
cout << "6 was rolled " << sum6 << " times." << endl;
return 0;
}
int Diceroll (int num) {
srand(time(0));
num = (rand()%6) + 1;
return num;
}