Back to the WFC main page

CRandomNumberGenerator2

$Revision: 6 $

Description

This class implements a better random number generator than the C runtime library. It uses the Mersenne Twister algorithm. Yehuda Hahn (ygh@cfsnet.com) wrote the C version adapted from http://www.math.keio.ac.jp/matumoto/cokus.c All I did was wrap it in a C++ class.

Methods

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 the generator.
void SetSeed( DWORD new_seed )
Sets the seed to this value. You can use this if you don't like my method of seeding.

Operators

These are what makes this class cool. You can use the class as a base type.

Example

#include <wfc.h>
#pragma hdrstop

void test_CRandomNumberGenerator( void )
{
   CRandomNumberGenerator2 random_number;

   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 );

      index++;
   }
}

API's Used

CRandomNumberGenerator2 uses the following API's:

Randomness Test

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-bits1,600,024,395
Number of 0-bits1,599,975,605
Longest series of 1-bits31
Longest series of 0-bits37
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 NumberNumber of 1's
050,001,806
149,996,289
249,998,465
349,995,963
450,008,188
549,991,291
650,010,640
750,004,980
850,002,946
949,997,908
1049,993,413
1150,004,827
1249,998,652
1349,993,790
1450,000,819
1549,995,340
1650,000,634
1750,004,450
1849,999,081
1950,003,646
2050,001,295
2150,005,936
2250,001,090
2350,003,166
2450,003,137
2549,999,151
2649,998,465
2750,001,554
2850,005,453
2950,010,749
3049,995,459
3149,995,812

Copyright, 2000, Samuel R. Blackburn
$Workfile: CRandomNumberGenerator2.cpp $
$Modtime: 2/05/00 8:27a $