This header file creates a vector of a size depending on the number of dice. It takes in number of rolls(rollsN), number of dice(numDice), and number of sides on the dice(numSides---fixed to 6). I think the problem is in the first for loop. It will run as intended when it is set to one dice, but terminate with out of range error when two dice is applied.
void randomNum(const int rollsN, int numDice, int numSides)
{
//Vector will hold an extra value (starts at 0, not 1).
vector<int> numVect((numDice*numSides) + 1);
//Starts a randomizer based on time
srand(time(0));
//provides random values for every possible result of the die
for(int i = 0; i < rollsN; i++)
{
int temp = 0; //holds the side of the dice that is chosen... or the sum of the two dice that are rolled
for(int j = 0; j < numDice; j++)
{
temp += (rand() % (numDice*numSides) + 1);
}
numVect.at(temp) += 1;
}
//prints how many times the die landed on that value
cout << endl << "RANDOMIZED RESULTS " << endl;
for(int i = 1; i <= (numDice*numSides); i++)
{
cout << i << " ----- " << numVect[i] << endl;
}
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl << "Histogram" << endl;
}
Aucun commentaire:
Enregistrer un commentaire