I'm having problems with my program's output. It keeps spitting out 12345.
Here's the details: It's split in three files: program8.cpp (the part that runs tests), myRandom.cpp (implementation of the class), and myRandom.h (specification of the class).
myRandom.h:
#ifndef MYRANDOM_H_ #define MYRANDOM_H_ class myRandom { public: myRandom(); //Constructor ~myRandom(); //Destructor void seed(unsigned long theSeed); //Mutator for current unsigned long next(); //Mutator or Accessor for current int randInt(int start, int end); //Scales result to a range double randNormal(); //Future expansion private: unsigned long current; //Current random # static const unsigned long a = 1103515245; //Multiplier for LGC static const unsigned long c = 12345; //Increment for LGC static const unsigned long m = 2147483648; //Modulus for LGC }; #endif /* MYRANDOM_H_ */ myRandom.cpp:
#include <iostream> #include <cstdlib> #include "myRandom.h" using namespace std; myRandom::myRandom() //Constructor { current = 0; } myRandom::~myRandom() //Destructor { } void myRandom::seed(unsigned long theSeed) //Mutator for current { if (theSeed < 0 || theSeed > m-1) { // ERROR return; } else current = theSeed; } unsigned long myRandom::next() //Mutator or Accessor for current { if (current < 0) { cout << "Error: cannot set seed to a negative number" << endl; return 0; } else { current = (m*current+c)%m; //Formula return current; } } int myRandom::randInt(int start, int end) //Scales result to a range { if (start >= end) { cout << "Error: cannot set start greater than or equal to end" << endl; return 0; } else { return ((this->next() % (end - start)) + start); } } double myRandom::randNormal() //Future expansion { cout << "Warning: randNormal not implemented" << endl; return 0; } program8.cpp:
#include <iostream> #include <cstdlib> #include "myRandom.h" using namespace std; int main() { myRandom theRand; unsigned long theSeed; cout << "Verify that the sequence generated by next() is the same on each run" << endl; for (int i = 0; i < 5; i++) { cout << theRand.next() << endl; } cout << "Verify that you can set the seed to 0 and 1" << endl; theSeed = 0; cout << theRand.next() << endl; theSeed = 1; cout << theRand.next() << endl; cout << "Verify that attempting to set the seed to -1 generates an error" << endl; theSeed = -1; cout << theRand.next() << endl; cout << "Verify that you can set the seed to m-2 and m-1" << endl; theSeed = 2147483648-2; cout << theRand.next() << endl; theSeed = 2147483648-1; cout << theRand.next() << endl; cout << "Verify that attempting to set the seed to m generates and error" << endl; theSeed = 2147483648; cout << theRand.next() << endl; cout << "Verify that next() produces a sequence predicted by hand/calc for the chosen seed" << endl; cout << "Please enter a seed: "; cin >> theSeed; cout << theRand.next() << endl; cout << "Verify that using start == end generates and error. Set both to 10." << endl; theRand.randInt(10,10); cout << theRand.next() << endl; cout << "Verify that using start > end generates and error. Set start to 10 and end to 5." << endl; theRand.randInt(10,5); cout << theRand.next() << endl; theRand.seed(theSeed); cout << "Testing randInt for start=0 end=1,000" << endl; for (int i = 0; i < 5; i++) { cout << theRand.randInt(0 , 1000) << endl; } return 0; } I think the problem lies in the next() function, since that's what gets called all those times in program8.cpp cout statements. I could understand getting 12345 once, but it should be updated once that function runs successive times. I apologize if it's a dumb question. Thank you for your time and patience.