Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a complete program written that will seed a random number generator, then

ID: 3619549 • Letter: I

Question

I have a complete program written that will seed a random number generator, then use the random numbers to flip a coin and roll a die. The program uses inheritance. The output should be the number of heads/tails using the coin and number of sides of the die that occur in 6000 turns. It is acting as if it gets the same random number 6000 times, not 6000 different random numbers. I did use srand( ). Any help finding my error is appreciated, hopefully they are small. Here is a sample of the output. I also posted my main(), base class, and one of the derived classes Enter a seed number: 1 Face Frequency Side Frequency 1 0 Heads 0 2 0 Tails 6000 3 0 4 0 5 0 6 6000 code main( ): cout > n; cout #include #include #include "aRandomNumberGenerator.h" using namespace std; // constructor is passed an integer aRandomNumberGenerator::aRandomNumberGenerator(int num) { setSeed(num); // call set function to initialize seed } // destructor aRandomNumberGenerator::~aRandomNumberGenerator() { } // sets the seed of the generator, has no return value void aRandomNumberGenerator::setSeed(int num) { seed = num; // store the number passed as the seed } // generates a set of 10 random numbers based off the seed int aRandomNumberGenerator::generate() { srand(seed); return rand(); } code derived class: // constructor aDie::aDie(int num, int t) : aRandomNumberGenerator( num ) { seed(t); } // destructor aDie::~aDie() { } // takes an integer argument and seeds the random number generator void aDie::seed(int t) { generatorSeed = t; } // receives a input from the user and outputs to screen int aDie::inputSeed() { return generatorSeed; } // uses the base class random number to generate 1-6 to represent the faces of a die int aDie::generate() { int y = aRandomNumberGenerator::generate(); return (1 + (y % 6)); } // rolls the die 1 time and returns 1-6 void aDie::roll() { int frequency1 = 0; // count of 1s rolled int frequency2 = 0; // count of 2s rolled int frequency3 = 0; // count of 3s rolled int frequency4 = 0; // count of 4s rolled int frequency5 = 0; // count of 5s rolled int frequency6 = 0; // count of 6s rolled int face; // stores most recently rolled value // summarize results for 6,000 rolls of a die for(int counter1 = 0; counter1 < 6000; counter1++) { face = generate(); // determine roll value 1-6 and increment appropriate counter switch ( face ) { case 1: ++frequency1; break; case 2: ++frequency2; break; case 3: ++frequency3; break; case 4: ++frequency4; break; case 5: ++frequency5; break; case 6: ++frequency6; break; default: cout << "7 doen't exist on a die!"; } } cout << "Face" << setw(13) << "Frequency" << endl; cout << endl; cout << "1" << setw(11) << frequency1 << endl; cout << "2" << setw(11) << frequency2 << endl; cout << "3" << setw(11) << frequency3 << endl; cout << "4" << setw(11) << frequency4 << endl; cout << "5" << setw(11) << frequency5 << endl; cout << "6" << setw(11) << frequency6 << endl; }

Explanation / Answer

i took c++ last year and a similar error happened to me. your code is too difficult to read, so im going to tell you what caused me the error. i was seeding the random number generator inside a loop and was setting it equal to a variable. my variable was initialized inside of my loop so it always reset the variable every time the loop reset. also my initialization was after my srand. check to see if your error is similar to mine.