At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Genetic Algorithms » Applications/Code

Randomal64 Pseudo Random Number Generator

By Andy Thomas

Description

Randomal64 is small and simple C++ class which implements a widely used pseudo random number generation algorithm, and is based on code appearing in "Random number generators: good nes are hard to find", by Stephen Park and Keith Miller. I wrote this this because I needed multiple and separately seeded generators for a project at the time, whereas the generator provided by the compiler was globally seeded. In addition, I extended the internal seed storage from 32 bits to 64 bits, thus increasing the range of unique 'random' sequences. This makes it suitable for use in simple 64 bit symmetric encryption algorithms. A description of each method in the class is provided below.

Randomal64 is written for Borland C++ Builder, however, it should be trivial to port it to virtually any platform. For example, to port it to standard C++, simply:

  1. Remove the __fastcall keywords,
  2. Change the __int64 type to one applicable to your platform,
  3. One method uses the Borland AnsiString class, replace this with string or char*.
See the following link for more detailed information about random number generators: http://www.math.utah.edu/~alfeld/Random/Random.html

This material is free of copyright and may be copied or used for any purpose without royalty. However, if distributed in a near-verbatim form, I would appreciate you including this notice.

Test Program

A distribution test program is available for Windows 32. This simple plots 'random' points on a 300x300 grid at positions x = rand()n, y = rand()n+1. A good generator should yield a 'random' and even distribution of points.

Randomal64 Methods

Randomal64(unsigned __int64 sd = 0);

Constructor with seed parameter. Acts as a default constructor also.

void seed(unsigned __int64 sd);

Seed generator. A seed value of zero causes the generator to be seeded with a time from the clock.

void stringSeed(const AnsiString& strSd);

Seed generator with a string (i.e. a password). The string can be of any length, but only strings of 8 characters or less will generate unique seed values.

void randomise();

Equivalent to calling seed(0).

int randInt(int range = 0x7FFFFFFF);

Returns a random 32 bit integer between 0 and range -1. This method can easily be modified to return a 64 bit number if you prefer.

double rand();

Return a random double value between 0 and 1.

bool flip();

Return random true or false.

static int roundToInt(double val);

Rounds a double to nearest integer value. I.e. 3.21 to 3, and 3.52 to 4.

Download Test Program & Source

Source: C++

Last Updated: 20/04/2001

Article content copyright © Andy Thomas, 2001.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -