Click here to Skip to main content
15,891,788 members
Articles / Programming Languages / C++
Article

Two little classes to easily get pseudo-random numbers

Rate me:
Please Sign up or sign in to vote.
3.94/5 (5 votes)
25 May 20041 min read 59.4K   1K   21   13
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"

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generala good work! Pin
yaofan12-Aug-07 23:54
yaofan12-Aug-07 23:54 
GeneralRe: a good work! Pin
Dlt7515-Nov-07 3:05
Dlt7515-Nov-07 3:05 
GeneralRandom Number from Sound Card Noise Pin
shiraztk3-Jun-04 22:32
shiraztk3-Jun-04 22:32 
GeneralRe: Random Number from Sound Card Noise Pin
Anonymous4-Jun-04 1:15
Anonymous4-Jun-04 1:15 
GeneralRe: Random Number from Sound Card Noise Pin
shiraztk4-Jun-04 2:53
shiraztk4-Jun-04 2:53 
GeneralRe: Random Number from Sound Card Noise Pin
Jeffrey Walton2-Jan-07 23:31
Jeffrey Walton2-Jan-07 23:31 
GeneralRe: Random Number from Sound Card Noise Pin
Dlt758-Jun-04 23:00
Dlt758-Jun-04 23:00 
GeneralRe: Random Number from Sound Card Noise Pin
shiraztk9-Jun-04 2:16
shiraztk9-Jun-04 2:16 
GeneralEven more RNGs Pin
msalters1-Jun-04 22:13
msalters1-Jun-04 22:13 
GeneralCRandom.... Pin
Member 1522221-Jun-04 21:42
Member 1522221-Jun-04 21:42 
GeneralBest seed in town.. Pin
Bamaco227-May-04 1:39
Bamaco227-May-04 1:39 
GeneralRe: Best seed in town.. Pin
Dlt7527-May-04 2:34
Dlt7527-May-04 2:34 
GeneralEasier way... Pin
Igor Vigdorchik26-May-04 6:47
Igor Vigdorchik26-May-04 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.