65.9K
CodeProject is changing. Read more.
Home

Two little classes to easily get pseudo-random numbers

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.94/5 (5 votes)

May 26, 2004

1 min read

viewsIcon

60149

downloadIcon

1019

Using two different generators, these classes provide good random numbers, bits and decimals

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:

  // constructors
  CRandom32(){
    m_liModulus  = 2147483647;  // 2^31 - 1
    m_liMultiplier = 16807;    // 7^5
    m_liRandomNumber = AutoSeed();
  }

  CRandom32(long int seed){
    m_liModulus  = 2147483647;
    m_liMultiplier = 16807;
    m_liRandomNumber = seed;
  }

  // returns the next number of the sequence
  long int NextNumber();
  // returns a random value in the range [0, max]
  long int NextLimitedNumber(long int max);
  // returns a decimal number in the range [0, 1]
  double NextDecimal();
  // returns a pseudo-random bit (0 or 1).
  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;  // counter...
  double dOnesPercentage;
  char cTempData[20];
  CRandom16 *p_Rnd16;
  CRandom32 *p_Rnd32;

  UpdateData();

  // checking input data
  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;
  }

  // getting random data
  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();

  // preparing to display the result
  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"