Textbook style program that generates the values of two, six-sided dice; which are used to perform simple calculations. Recently, I saw an example using similar criteria that was able to utilize 360,000,000 rolls. However, I have not been able to initialize an array
or vector
containing more than 100 million elements.
Assuming typical settings/RAM, can this be accomplished without an intermediary step? I.E. creating two arrays
, generating the values of the first, storing the calculations, and then destroying it before repeating the process. Other criticisms are welcome and encouraged. Thanks in advance.
The code below will support (tested) up to 36,000,000 on my machine; albeit it averages 58 seconds to run.
#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::count;
using std::exception;
using std::vector;
int rollDice();
bool running(true);
char choice;
int main() {
while (running) {
bool invalidChoice(true);
srand((unsigned int)time(NULL));
int numberRolls;
double odds[11], percentage[11];
int occurrence[11], possibleSums[11];
// Odds for possible sums 2-12 for 2 dice. Index is # out of 36.
int possibleOdds[11] = {1,2,3,4,5,6,5,4,3,2,1};
vector<int> numberValues;
cout << "How many times will the dice roll?\n" << endl;
cin >> numberRolls;
try {
vector<int> numberValues(numberRolls);
for (int i = 0; i <= numberRolls; i++) {
numberValues.push_back(rollDice());
}
cout << "\nSimulating : " << numberRolls << " rolls...\n" << endl;
cout << setw(6) << "Sum" << setw(18) << "#Rolled" << setw(18) << "Odds" << setw(18) << "%Error" << endl;
int p = 2;
while (p < 12) {
for (int currentIndex = 0; currentIndex < 11; currentIndex++) {
possibleSums[currentIndex] = p;
occurrence[currentIndex] = count(numberValues.begin(), numberValues.end(), p);
if (numberRolls <= 36) {
odds[currentIndex] = possibleOdds[currentIndex];
}
// Scales odds for any number of dice rolls greater than 36 (Non-Multiples of 36)
else {
odds[currentIndex] = numberRolls / (((double)36) / ((double)possibleOdds[currentIndex]));
}
percentage[currentIndex] = ((((double)occurrence[currentIndex]) - ((double)(odds[currentIndex]))) / ((double)(odds[currentIndex]))) * 100;
cout << setw(6) << possibleSums[currentIndex] << setw(18) << occurrence[currentIndex] << setw(18) << odds[currentIndex] << setw(18) << percentage[currentIndex] << endl;
p++;
}// Close for
} // Close while
} // Close try
// Generalized Catch
catch (exception & e) {
cerr << "EXCEPTION CAUGHT : " << e.what() << '\n';
}
// Option to run again
while (invalidChoice) {
cout << "\nRun Again? (y/n): \n " << endl;
cin >> choice;
if (choice == 'y') {
invalidChoice = false;
cout << "\nSelected: \"" << choice << "\" *** RUNNING AGAIN. ***\n" << endl;
}
else if (choice == 'n') {
invalidChoice = false;
running = false;
cout << "\nSelected: \"" << choice << "\" *** EXITING. ***\n" << endl;
}
else {
cerr << "\nERROR: The only valid answers are y/n. \n" << endl;
}
}// Close option
}// Close running
// Pause before returning
system("pause");
return 0;
}// End Main
// Function to produce two dice values; returning their sum
int rollDice() {
int dieI = (rand() % 6) + 1;
int dieII = (rand() % 6) + 1;
int sum = dieI + dieII;
return sum;
}