Click here to Skip to main content
13,299,727 members (53,578 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 25 May 2004

Two little classes to easily get pseudo-random numbers

, 25 May 2004
Rate this:
Please Sign up or sign in to vote.
Using two different generators, these classes provide good random numbers, bits and decimals


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.


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
  long int m_liModulus;
  long int m_liMultiplier;
  long int m_liRandomNumber;

  long int AutoSeed();

  // constructors
    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;


  // checking input data
  if (m_radRND16.GetCheck())
    p_Rnd16 = new CRandom16();
  else if (m_radRND32.GetCheck())
    p_Rnd32 = new CRandom32();
    ::MessageBox(0, "Select Generator!", "Info", MB_OK);
  liBitsForStream = atol(m_strBitsForStream.GetBuffer(1));
  if (liBitsForStream <=0 ){
    ::MessageBox(0, "Set Number Of Bits!", "Info", MB_OK);

  // getting random data
  if (m_radRND16.GetCheck())
    for (i=0 ; i<liBitsForStream ; i++)
    for (i=0 ; i<liBitsForStream ; i++)

  // preparing to display the result
  dOnesPercentage = (double) liNumberOfOnes / liBitsForStream;
  sprintf(cTempData, "%f", dOnesPercentage);
  m_strPercentage = cTempData;

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"


#include "time.h"


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


About the Author

Italy Italy
No Biography provided

You may also be interested in...


Comments and Discussions

Generala good work! Pin
yaofan13-Aug-07 0:54
memberyaofan13-Aug-07 0:54 
GeneralRe: a good work! Pin
Dlt7515-Nov-07 4:05
memberDlt7515-Nov-07 4:05 
GeneralRandom Number from Sound Card Noise Pin
zainu3-Jun-04 23:32
memberzainu3-Jun-04 23:32 
GeneralRe: Random Number from Sound Card Noise Pin
Anonymous4-Jun-04 2:15
sussAnonymous4-Jun-04 2:15 
GeneralRe: Random Number from Sound Card Noise Pin
zainu4-Jun-04 3:53
memberzainu4-Jun-04 3:53 
GeneralRe: Random Number from Sound Card Noise Pin
Jeffrey Walton3-Jan-07 0:31
memberJeffrey Walton3-Jan-07 0:31 
GeneralRe: Random Number from Sound Card Noise Pin
Dlt759-Jun-04 0:00
memberDlt759-Jun-04 0:00 
GeneralRe: Random Number from Sound Card Noise Pin
zainu9-Jun-04 3:16
memberzainu9-Jun-04 3:16 
GeneralEven more RNGs Pin
msalters1-Jun-04 23:13
membermsalters1-Jun-04 23:13 
GeneralCRandom.... Pin
f.r.e.d.1-Jun-04 22:42
memberf.r.e.d.1-Jun-04 22:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 26 May 2004
Article Copyright 2004 by Dlt75
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid