Click here to Skip to main content
Click here to Skip to main content

Two little classes to easily get pseudo-random numbers

By , 25 May 2004
Rate this:
Please Sign up or sign in to vote.

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

About the Author

Dlt75
Engineer
Italy Italy
No Biography provided

Comments and Discussions

 
Generala good work! Pinmemberyaofan12-Aug-07 23:54 
GeneralRe: a good work! PinmemberDlt7515-Nov-07 3:05 
GeneralRandom Number from Sound Card Noise Pinmemberzainu3-Jun-04 22:32 
GeneralRe: Random Number from Sound Card Noise PinsussAnonymous4-Jun-04 1:15 
GeneralRe: Random Number from Sound Card Noise Pinmemberzainu4-Jun-04 2:53 
GeneralRe: Random Number from Sound Card Noise PinmemberJeffrey Walton2-Jan-07 23:31 
GeneralRe: Random Number from Sound Card Noise PinmemberDlt758-Jun-04 23:00 
GeneralRe: Random Number from Sound Card Noise Pinmemberzainu9-Jun-04 2:16 
GeneralEven more RNGs Pinmembermsalters1-Jun-04 22:13 
GeneralCRandom.... Pinmemberf.r.e.d.1-Jun-04 21:42 
GeneralBest seed in town.. PinmemberBamaco227-May-04 1:39 
GeneralRe: Best seed in town.. PinmemberDlt7527-May-04 2:34 
GeneralEasier way... PinmemberIgor Vigdorchik26-May-04 6:47 

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

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

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