Reputation: 2016
This is written to simulate 2 6-sided dice being thrown. But when I put in 10 throws, it randomly throws as many as it wants to (4, 5, 6 etc.) . Am I missing something?
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
int a10 = 0;
int a11 = 0;
int a12 = 0;
for (int i = 0; i < y; i++)
{
throwDice();
if (throwDice() == 2)
a2++;
else if (throwDice() == 3)
a3++;
else if (throwDice() == 4)
a4++;
else if (throwDice() == 5)
a5++;
else if (throwDice() == 6)
a6++;
else if (throwDice() == 7)
a7++;
else if (throwDice() == 8)
a8++;
else if (throwDice() == 9)
a9++;
else if (throwDice() == 10)
a10++;
else if (throwDice() == 11)
a11++;
else if (throwDice() == 12)
a12++;
}
cout << "2 = " << a2 << endl;
cout << "3 = " << a3 << endl;
cout << "4 = " << a4 << endl;
cout << "5 = " << a5 << endl;
cout << "6 = " << a6 << endl;
cout << "7 = " << a7 << endl;
cout << "8 = " << a8 << endl;
cout << "9 = " << a9 << endl;
cout << "10 = " << a10 << endl;
cout << "11 = " << a11 << endl;
cout << "12 = " << a12 << endl;
system("pause");
}
Upvotes: 1
Views: 316
Reputation: 22318
I know this doesn't answer the question, but I couldn't help but wonder if you might benefit from C++11 if it's available to you:
#include <random>
std::minstd_rand prng;
std::uniform_int_distribution<int> dice (1, 6);
int throwDice ()
{
return (dice(prng) + dice(prng));
}
int main ()
{
prng.seed(time(NULL));
// (code)
}
Upvotes: 0
Reputation: 18443
I would write this code instead:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int total[13];
for( int i = 2; i <= 12; i++ )
total[i] = 0;
for (int i = 0; i < y; i++)
{
total[throwDice()]++;
}
for (int i = 2; i <= 12; i++)
cout << i << " = " << total[i] << endl;
system("pause");
}
Much simpler and more understandable.
Well, here are the problems with your code:
You are calling the throwDice
function repeatedly in all the if
statements. You only need to call it once in each iteration of the loop, store the result and compare the result. You should not call it every time for comparition. Every call gives you a new result.
Also you can see in my code that how using an array simplifies the whole code. Here I am wasting two array elements (0 and 1) which can be avoided by a simple arithmetic with the index.
Upvotes: 2
Reputation: 681
Your loop should look like
int result;
for (int i = 0; i < y; i++)
{
result = throwDice();
if (result == 2)
a2++;
else if (result == 3)
a3++;
else if (result == 4)
a4++;
else if (result == 5)
a5++;
else if (result == 6)
a6++;
else if (result == 7)
a7++;
else if (result == 8)
a8++;
else if (result == 9)
a9++;
else if (result == 10)
a10++;
else if (result == 11)
a11++;
else if (result == 12)
a12++;
}
Also, the throwDice()
function is not equivalent to throwing 2 dice. You have created a function that will have an equal chance of rolling all values from 2 to 12. When you roll 2 dice, it is far more likely to roll a 6, for instance, than a 12. You should be creating two numbers between 1 and 6 and adding them to get the return value for the throwDice()
function.
Upvotes: 1
Reputation: 16253
You are calling throwDice()
once to generate the throw (correct) and then again in every evaluated condition in your if
statements (incorrect). Save the result of the throw in a variable and use that variable in your check.
Your throwDice
function does not simulate two 6-sided dice being thrown, but it simulates one 11-sided die (with numbers 2-11 printed on it) being thrown. That makes a difference. (This is not a programming problem though, but a mathematical one. Once you understand the maths behind it, it's easy to correct your function.)
Upvotes: 6