0

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.

1 Answer 1

1

Your problem isn't a code specific one - it is Math-related from here:

current = (m*current+c)%m; 

This always returns the value of c if c < m, otherwise (or more generally) it returns c % m. Why? From this theorem:

(m*n + a)%m = a 

Example:

m = 10 n = 3 a = 7 (10*3 + 7)%10 = 7 

See this for more:

http://en.wikipedia.org/wiki/Modulo_operation

Sign up to request clarification or add additional context in comments.

9 Comments

Well, this always returns c only if c < m.
You can see it this way: (m * current + c) % m = ((m * current) % m) + c % m = 0 + c % m. :)
@Jefffrey: You're very correct - thanks for mentioning that. I've edited it. :)
So to fix this, I can just check if c < m, and if it's not, run the formula, otherwise return an error?
Note that your theorem is correct in maths, but for some reason computer science does weird things with negative numbers and it won't work. In particular if you choose a negative n.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.