Click here to Skip to main content
12,951,971 members (68,085 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 4 Jun 2002

Simple Random Class (EasyRandom)

, 4 Jun 2002
Rate this:
Please Sign up or sign in to vote.
A simple class to use in your MFC project to generate random numbers within an interval.
<!-- Download Links --> <!-- Article image -->

Sample Image - EasyRandom.jpg

<!-- Add the rest of your HTML here -->


I searched the net for ages looking for sample classes & examples to generate a random number between two values. (Example - between 1 and 100). I wrote a MFC application where riddles are used to check whether a temporary employee, working the evening shift, is awake. The entity must respond within a certain time frame to the riddle, otherwise he is either sleeping or playing Return to Castle Wolfenstein. Sorry! Back to the problem - The riddles are read in from a text file, but I needed to randomize the riddles so that every time the application starts a random riddle is generated.

I developed a very simple class called EasyRandom and can (should) be easily integrated in your project.

The seed for the pseudo-random number is generated using the current local system time and date. This means random numbers generated will always be different, even if the application is scheduled to start at set intervals in a day.

The sample project I include is just to show you the workings and how the class file is added to the MFC project.

Using the class

  • In file view, add the EasyRand.cpp file to your project.
  • Still in file view, double-click on <YourProject>Dlg.cpp and add the following code segment in the declaration section: (in BOLD)
    #include "stdafx.h"
    #include "EasyRand.h"
  • In the In the sample application, I used a Randomize button to generate my random numbers, I'll use the OnRandom function (called when the randomize button is pressed) to demonstrate the class usage:
    void CTestEasyRandomDlg::OnRandom() 
       CString s;
       EasyRandom RandomX(m_nLow,m_nHigh);
       for (int i = 0; i < m_nAmount; i++) 
          s.Format("%d", RandomX.DrawRandomNumber());
  • EasyRandom RandomX(m_nLow, m_nHigh); This is the general interface to the EasyRandom class. This line creates the object RandomX and passes the Low and High values to the constructor
  • RandomX.SetTimerSeed(); This statement sets the random seed for object RandomX. (The seed is generated using the local system time + date)
  • RandomX.DrawRandomNumber(); Returns a (one) random number.


Here is a summary of the public interfaces of the class...

// default conmstructor with defaults of 0 & 1
EasyRandom(int a=0, int b=1);

// Mutator:
// Sets high and low values
void SetInterval(int a, int b);

// Faclitators:
// Display Random numbers...
int DrawRandomNumber();
// Set seed using current time
void SetTimerSeed();

Recap - 3 easy steps:

  1. Ceate an EasyRandom object
  2. call SetTimerSeed() to initialise the "random" seed.
  3. call DrawRandomNumber() for output


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

Web Developer
South Africa South Africa
I am an IT security consultant that focuses mainly in Oracle, Microsoft, Citrix, RSA, SUN, and Linux security. What I do is Perimeter security design (firewalls, IDS etc) as well as internal / external network assessments (penetration).

On the programming side I’m sellable on VC++. I’m also strong using c# ( TSQL, VC++.Net, STL, COM, ATL, Java/VBscript / Coldfusion /

Home page:

You may also be interested in...

Comments and Discussions

GeneralCStdLib rand() Pin
Darek Nawrocki9-Sep-06 9:08
memberDarek Nawrocki9-Sep-06 9:08 
GeneralRe: CStdLib rand() Pin
Darek Nawrocki9-Sep-06 9:10
memberDarek Nawrocki9-Sep-06 9:10 
GeneralRe: CStdLib rand() Pin
nums11-Sep-06 6:56
membernums11-Sep-06 6:56 
Generalcould just put in a function like this .. Pin
Bill Gates Antimatter Particle23-Aug-02 1:11
memberBill Gates Antimatter Particle23-Aug-02 1:11 
GeneralRe: could just put in a function like this .. Pin
Tim Smith23-Aug-02 1:59
memberTim Smith23-Aug-02 1:59 
GeneralPoor random number generator Pin
Philippe Lhoste11-Jun-02 2:35
memberPhilippe Lhoste11-Jun-02 2:35 
GeneralRe: Poor random number generator Pin
nums11-Jun-02 3:39
membernums11-Jun-02 3:39 
GeneralRe: Poor random number generator Pin
Philippe Lhoste11-Jun-02 3:59
memberPhilippe Lhoste11-Jun-02 3:59 
GeneralRe: Poor random number generator Pin
Tim Smith23-Aug-02 2:01
memberTim Smith23-Aug-02 2:01 
GeneralRPG dice Pin
SunKnight5-Jun-02 15:37
memberSunKnight5-Jun-02 15:37 
GeneralSleeping Employees Pin
kevnar5-Jun-02 7:02
memberkevnar5-Jun-02 7:02 
GeneralRe: Sleeping Employees Pin
nums5-Jun-02 21:06
membernums5-Jun-02 21:06 
GeneralRe: Sleeping Employees Pin
kevnar6-Jun-02 6:10
memberkevnar6-Jun-02 6:10 
GeneralRe: Sleeping Employees Pin
nums11-Jun-02 3:51
membernums11-Jun-02 3:51 

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
Web02 | 2.8.170525.1 | Last Updated 5 Jun 2002
Article Copyright 2002 by nums
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid