Thread: Dice random generator problem

1. Dice random generator problem

The purpose of this problem is to simulate the tossing of two dice using an array of ints and the C++ random number generator rand(). When a pair of dice is tossed, the total number of spots showing on their faces are counted and this count is used as the index into an array consisting of 13 int elements. The random number generator will produce an int number from 0 to 12. A number which is either 0 or 1 is discarded. All other numbers are used as indexes into the array elements. The values of all array elements will be initialized to 0. Each indexed array element will be incremented by 1. The program will run until a total of 1,000 increments have been applied to array elements 2 through 12. Then, the program will display the counts in each of the array elements 2 through 12 and terminate.

Any help I can get would be greatly appreciated, I am kinda new at this and to get off on the right foot would be a lot of help, thanks!

2. What have you tried?

3. re

I tried this, but I think I am way off base:

Code:
```#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int rollDice();

int main(int argc, char* argv)
{
int numRolls;
int arrRolls[12];
int dieRoll;

srand((unsigned)time(0));

for (int k = 0; k < 12; k++)
{
arrRolls[k] = 0;
}

for (int i = 1; i <= numRolls; i++)
{
dieRoll = rollDice();
cout << "Die Roll " << i << ": " << dieRoll << endl;
arrRolls[dieRoll - 1]++;
}

for (int j = 0; j < 12; j++)
{
cout << "The number " << j + 1 << " was rolled " << arrRolls[j] << " times." << endl;
}
}

int rollDice()
{
int result;

result = (rand() % 12) + 1;

return result;
}```

4. Why are you asking how many times the dice are to be rolled? That's fixed in the problem statement. You're still allowing a roll of 1 to happen. The specs state that you need to have an array with 13 elements in it, and also that the dice roll itself must be used as the index element.

Popular pages Recent additions