| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
Randomal64 Pseudo Random Number GeneratorBy Andy Thomas
DescriptionRandomal64 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:
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 ProgramA 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 MethodsRandomal64(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
Last Updated: 20/04/2001 Article content copyright © Andy Thomas, 2001.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -