Reputation: 3
I am trying to implement a class that can generate a pseudorandom sequence.
The constructor should have and initial seed, multiplier, increment, and modulus as parameters. One member function should permit the seed to be changed, one function should generate and return the next number in the pseudorandom sequence.
My results are erroneous. What am I doing wrong, and what should be the correct implementations.
Header file for the pseudorandom sequence:
#include<iostream>
using namespace std;
class pRandInt
{
public:
pRandInt();
//Default constructor with parameters
pRandInt(int, int, int, int);
//intial number in pseudorandom sequence
//permits the seed to be changed
void setFirstNum(int);
//generate the next number in the pseudorandom sequence
int getNextNum();
private:
int newSeed;
int newMulti;
int newIncr;
int newMod;
};
Implementation file for the pseudorandom sequence:
#include "pRandInt.h"
pRandInt::pRandInt()
{
int newSeed = 0;
const int newMulti = 40;
const int newIncr = 725;
const int newMod = 729;
}
pRandInt::pRandInt(int seed, int multi, int incr, int mod)
{
newSeed = seed;
newMulti = multi;
newIncr = incr;
newMod = mod;
}
void pRandInt::setFirstNum(int seed)
{
newSeed = seed;
}
int pRandInt::getNextNum()
{
return (newMulti * newSeed + newIncr) % newMod;
}
Main test file for the pseudorandom sequence:
#include <iostream>
#include "pRandInt.h"
using namespace std;
int main()
{
int seed = 0;
pRandInt num;
num.setFirstNum(seed);
cout << "The first number in your sequence is: ";
cin >> seed;
cout << "The other numbers in your sequence are: ";
cout << num.getNextNum() << endl;
system("pause");
return 0;
}
Upvotes: 0
Views: 109
Reputation: 882146
Your problem lies here:
int pRandInt::getNextNum()
{
return (newMulti * newSeed + newIncr) % newMod;
}
If you look closely at that function and how it's called, you'll notice none of those values ever change. It's customary (some would say necessary) to change one of them if you ever want to see a different output.
My suggestion would be to look into the seed :-)
Short of actually writing the code for you (something I tend to frown upon when trying to answer obviously educational questions on SO), that's about as much help as I can give. However, it should be more than enough for you to solve it yourself.
Upvotes: 1