65.9K
CodeProject is changing. Read more.
Home

Simple Random Class (EasyRandom)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (5 votes)

Jun 5, 2002

2 min read

viewsIcon

99481

downloadIcon

1869

A simple class to use in your MFC project to generate random numbers within an interval.

Sample Image - EasyRandom.jpg

Introduction

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;
       UpdateData();
       m_cRandomBox.ResetContent();
       EasyRandom RandomX(m_nLow,m_nHigh);
       RandomX.SetTimerSeed();
       for (int i = 0; i < m_nAmount; i++) 
       {
          s.Format("%d", RandomX.DrawRandomNumber());
          m_cRandomBox.AddString(s);
       }
       UpdateData(FALSE);
    }
  • 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.

Summary

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