Back to Home Page

/********************************************************************************
In this project you will design and implement a class that can generate a sequence
of pseudorandom integers, which is a sequence that appears random in many ways. The
approach uses the linear congruence method, explained below.
The linear congruence method starts with a number called the seed. In addition to the seed,
three other numbers are used in the linear congruence method, called the multiplier,
the increment, and the modulus. The formula for generting a sequence of pseudorandom
numbers is quite simple. The first number is:

(multiplier * seed + increment) % modulus

This formula uses the C++ % operator, which computes the remainder from an integer
division.
Each time a new random number is computed, the value of the seed is changed to that new number.
For example, we could implement a pseudorandom number generator with multiplier = 40,
increment = 3641, and modulus = 729. If we choose the seed to be 1, then the sequence of numbers
will proceed as shown here:

First number
=(multiplier * seed + increment) % modulus
=(40 * 1 + 3641) % 729
=36
and 36 becomes the new seed

Next number
=(multiplier * seed + increment) % modulus
=(40 * 36 + 3641) % 729
=707
and 707 becomes the new seed

Next number
=(multiplier * seed + increment) % modulus
=(40 * 707 + 3641) % 729
=574
and 574 becomes the new seed, and so on

These particular values for multiplier, increment, and modulus happen to be good
choices. The pattern generated will not repeat until 729 different numbers have been
produced. Other choices for the constants might not be so good.
For this project, design and implement a class that can generate a pseudorandom sequence
in the manner described. The initial seed, multiplier, increment, and modulus should all
be parameters of the constructor. There should also be a member function to permit the seed
to be changed, and a member function to generate and return the next number in the
pseudorandom sequence.

View Output

********************************************************************************/

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include "Pseudo.h"
//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
Pseudorandom MySequence(1,40,3641,729);
cout<<"First Number"<<endl;
cout<<MySequence.next_number()<<endl;
cout<<endl;
for (int i = 1; i<=3;i++)
{
cout<<"Next number"<<endl;
cout<<MySequence.next_number()<<endl;
cout<<endl;
}
getchar();
return 0;
}
//---------------------------------------------------------------------------

//Pseudo.cpp file

#include <math.h>
#include "Pseudo.h"
//CONSTRUCTORS
Pseudorandom ::Pseudorandom ( int ini_seed,int ini_multiplier,int ini_increment,int ini_modulus)
{
seed = ini_seed;
multiplier = ini_multiplier;
increment = ini_increment;
modulus = ini_modulus;
}

//MODIFICATION MEMBER FUNCTIONS
void Pseudorandom ::change_seed (int new_seed)
{
seed = new_seed;
}
int Pseudorandom ::next_number()
{
int next ;
next = ( (multiplier * seed) + increment) % modulus ;
seed = next;
return next;
}

//************************************************************************************************

//Pseudo.h Header File

#ifndef Pseudo_H
#define Pseudo_H

class Pseudorandom
{
public:
//CONSTRUCTORS

Pseudorandom ::Pseudorandom ( int ini_seed,int ini_multiplier,int ini_increment,int ini_modulus);

//MODIFICATION MEMBER FUNCTIONS

void Pseudorandom ::change_seed (int new_seed);
int Pseudorandom ::next_number();

//CONSTANT MEMBER FUNCTIONS

private:
int seed;
int multiplier;
int increment;
int modulus;
};

#endif

View Output

//*****************************************************************************************

Back To Home Page


Copyright  © 2002                                   GJ  GUILLERMO JULCA