Back to the WFC main page
$Revision: 6 $
This class implements a better random number generator than the C runtime
library. It uses the Mersenne Twister algorithm. Yehuda Hahn
) wrote the C version
All I did was wrap it in a C++ class.
DWORD GetInteger( void )
Returns a 32-bit random integer.
double GetFloat( void )
Returns a random float between 0 and 1.
void InitializeSeed( void )
Initializes the seed value for the generator. To increase randomness,
it uses a combination of the number of milliseconds the NT machine has
been on and the number of free clusters on the current drive to seed
void SetSeed( DWORD new_seed )
Sets the seed to this value. You can use this if you don't like my
method of seeding.
These are what makes this class cool. You can use the class as a
void test_CRandomNumberGenerator( void )
int index = 0;
while( index < 20 )
_tprintf( TEXT( "DWORD random number is %lu\n" ), (DWORD) random_number );
_tprintf( TEXT( "double random number is %lf\n" ), (double) random_number );
uses the following API's:
I'm no mathematician. If you are, please have mercy on me when you
read the results of my randomness tests on this generator.
I ran this generator through a few tests that I could think of for
randomness. Here's the results for generating 100,000,000 32-bit integers
(that makes 3,200,000,000 bits).
|Number of 1-bits||1,600,024,395|
|Number of 0-bits||1,599,975,605|
|Longest series of 1-bits||31|
|Longest series of 0-bits||37|
Here's a breakdown of the bits in each of the 32 slots that
make up a 32-bit integer. The count is the number of times
the bit at that location had a value of 1.
|Bit Number||Number of 1's|
Copyright, 2000, Samuel R. Blackburn
$Workfile: CRandomNumberGenerator2.cpp $
$Modtime: 2/05/00 8:27a $