Introduction
Often in my applications I had to use random numbers, so I wrote my own classes to get
the data I needed. I wanted a class that was little, easy to use, but with quite good pseudo-random properties.
Details
The two classes have the same method names and the only difference is on the pseudo-random number generator used.
Let's take a look at the CRandom32
class:
class CRandom32
{
private:
long int m_liModulus;
long int m_liMultiplier;
long int m_liRandomNumber;
long int AutoSeed();
public:
CRandom32(){
m_liModulus = 2147483647;
m_liMultiplier = 16807;
m_liRandomNumber = AutoSeed();
}
CRandom32(long int seed){
m_liModulus = 2147483647;
m_liMultiplier = 16807;
m_liRandomNumber = seed;
}
long int NextNumber();
long int NextLimitedNumber(long int max);
double NextDecimal();
short int NextBit();
};
The generator that I use for this class is good and provides pseudo-random numbers (with uniform distribution) in the range [0, 2^31 - 2], with a maximum sequence length of 2^31 - 1 (using NextNumber
method). Moreover, you can also create a pseudo-random bit stream (using NextBit()
), get a random decimal number in the range [0, 1] (using NextDecimal
) or get random byte sequences (using NextLimitedNumber(255)
).
If you don't provide an initial seed for the internal number generator, the method AutoSeed()
will calculate one
for you using local time (function time(0)
) and cpu time (function clock()
).
The CRandom16
class does the same things, but using the well known standard C random number generator (rand()
function).
Using the code
The use of this class is very simple and the best way to understand it is an example, so let's take
a look at the demo project (when you press "Calculate" button, the OnButtonCalculate
method is called)
#include "stdafx.h"
#include "CRandomDemo.h"
#include "CRandomDemoDlg.h"
#include "random16.h"
#include "random32.h"
...
void CCRandomDemoDlg::OnButtonCalculate()
{
long int liBitsForStream;
long int liNumberOfOnes = 0;
long int i;
double dOnesPercentage;
char cTempData[20];
CRandom16 *p_Rnd16;
CRandom32 *p_Rnd32;
UpdateData();
if (m_radRND16.GetCheck())
p_Rnd16 = new CRandom16();
else if (m_radRND32.GetCheck())
p_Rnd32 = new CRandom32();
else{
::MessageBox(0, "Select Generator!", "Info", MB_OK);
return;
}
liBitsForStream = atol(m_strBitsForStream.GetBuffer(1));
if (liBitsForStream <=0 ){
::MessageBox(0, "Set Number Of Bits!", "Info", MB_OK);
return;
}
if (m_radRND16.GetCheck())
for (i=0 ; i<liBitsForStream ; i++)
liNumberOfOnes+=p_Rnd16->NextBit();
else
for (i=0 ; i<liBitsForStream ; i++)
liNumberOfOnes+=p_Rnd32->NextBit();
dOnesPercentage = (double) liNumberOfOnes / liBitsForStream;
sprintf(cTempData, "%f", dOnesPercentage);
m_strPercentage = cTempData;
UpdateData(FALSE);
}
As you can see, you just have to call constructor and then call the method that gives you what you need (a random bit, in this case).
If you are not using these classes into a MFC application, in random16.cpp and random32.cpp you just have to substitute:
#include "stdafx.h"
with
#include "time.h"