-2
該頭文件根據骰子的數量創建一個大小的向量。它需要擲骰數(擲骰數N),骰數(numDice)和骰子邊數(numSides ---固定爲6)。我認爲問題出現在第一個for循環中。當它被設置爲一個骰子時,它將按照預期運行,但當應用兩個骰子時,它會以超出範圍的錯誤終止。一個或兩個骰子的隨機數發生器
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;
}
你不覺得'numVect'的尺寸應的骰子次數輥的數量?我看不出有多少方面需要處理...... –
'srand(time(0));'應該只做一次*一次,通常在程序啓動時。 – paxdiablo