Psuedorandom Code generator

javascript:tx('Having some difficulties with a program. The purpose of the program is to generate psuedorandom numbers. The first set of numbers is random. The equation is (multiplier * seed + increment) % modulus. The result of this is suppose to be fed back into the equation as the seed, with the multiplier, increment, and modulus staying the same. This is to generate a series of numbers that appear random but still have a pattern. I have written separate files for the header file, implementation file, and main file. Unfortunately my numbers are not changing.
Here is the result:')
javascript:tx('Current seed is: 12840
Multiplier is: 4198
Incremental value is: 3698
Modulus is: 1461
Current seed is: 12840
Multiplier is: 4198
Incremental value is: 3698
Modulus is: 1461
Current seed is: 12840
Multiplier is: 4198
Incremental value is: 3698
Modulus is: 1461
Current seed is: 12840
Multiplier is: 4198
Incremental value is: 3698
Modulus is: 1461
Current seed is: 12840
Multiplier is: 4198
Incremental value is: 3698
Modulus is: 1461')

Header file

javascript:tx('
#ifndef HW1CLASSFILE1_H //protection from duplicating the header file
#define HW1CLASSFILE1_H

class randGenerator //here is my class for my psuedorandom number generator
{
public:
randGenerator(); //this is my constructor that I will define in my implementation file
void setMult(); //this is the void function that sets the multiplier variable
int setSeed(); //this is the void function that sets the seed
void setMod(); //this is the void function that sets the modulus variable
void setInc(); //this is the void function to set the incremental value
int calcNum(int); //here is the function that I will define to calculate the random numbers
void printNum(int); //this is the function to print all the values
private:
int multiplier; //private object for the multiplier
int modulus1; //private object for the modulus
int increment; //private object for the incremental value
};

#endif
')

Implementation file

javascript:tx('
#include "hw1classfile.h"
#include <cmath>
#include <stdlib.h>
#include <iostream>

using namespace std;
randGenerator::randGenerator() //here is my constructor that initializes all my private objects
{
multiplier = 0;
increment = 0;
modulus1 = 0;
}

void randGenerator::setMult() //this is my void function that calculates the random variable for the multiplier
{
const int mult = rand();
multiplier = mult;
}

void randGenerator::setInc() //this is my void function that calculates the random variable for the incremental value
{
const int incr = rand();
increment = incr;
}

void randGenerator::setMod() //this is the void function that calculates the modulus
{
const int modu = rand();
modulus1 = modu;
}
int randGenerator::setSeed() //this is the return function that is used in the main program to define the initial seed
{
int seed = rand();
return seed;
}

int randGenerator::calcNum(int x) //here is my function that calculates the updated seed, in main it will be in the loop
{
int temp = 0;
temp = ((multiplier*x) + increment) % modulus1;
x = temp;
return x;
}

void randGenerator::printNum(int x) //here is the function for printing the original random values and updated seed
{
cout << "Current seed is: " << x << endl;
cout << "Multiplier is: " << multiplier << endl;
cout << "Incremental value is: " << increment << endl;
cout << "Modulus is: " << modulus1 << endl;
}
')

Main file

javascript:tx('
#include <time.h>

using namespace std;

int main()
{
srand(time(NULL));
int x;
randGenerator randomNumber;
randomNumber.setMult();
randomNumber.setInc();
randomNumber.setMod();
x = randomNumber.setSeed();

for (int i = 0; i <= 9;i++)
{
randomNumber.calcNum(x);
randomNumber.printNum(x);
i++;
}
cin.get();
cin.get();
return 0;
}')

I'm not sure what I'm doing wrong. Any advice is appreciated.
Neither of the functions you call within the loop modify the state of your generator, so it generates the same number over and over.
- Why not use constructor initialiser lists?
http://en.cppreference.com/w/cpp/language/initializer_list

1
2
3
4
5
6
7
8
9
void randGenerator::setMult() //this is my void function that calculates the random variable for the multiplier
{
        // why type 2 lines when you can type 1?
        const int mult = rand();
        multiplier = mult;

        // fixed
        multiplier = rand();
}

Topic archived. No new replies allowed.