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.
char
unsigned char
int
unsigned int
short
unsigned short
long
unsigned long
float
double
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:
- GetDiskFreeSpace
- GetSystemTime
- GetTickCount
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-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 |
0 | 50,001,806 |
1 | 49,996,289 |
2 | 49,998,465 |
3 | 49,995,963 |
4 | 50,008,188 |
5 | 49,991,291 |
6 | 50,010,640 |
7 | 50,004,980 |
8 | 50,002,946 |
9 | 49,997,908 |
10 | 49,993,413 |
11 | 50,004,827 |
12 | 49,998,652 |
13 | 49,993,790 |
14 | 50,000,819 |
15 | 49,995,340 |
16 | 50,000,634 |
17 | 50,004,450 |
18 | 49,999,081 |
19 | 50,003,646 |
20 | 50,001,295 |
21 | 50,005,936 |
22 | 50,001,090 |
23 | 50,003,166 |
24 | 50,003,137 |
25 | 49,999,151 |
26 | 49,998,465 |
27 | 50,001,554 |
28 | 50,005,453 |
29 | 50,010,749 |
30 | 49,995,459 |
31 | 49,995,812 |
Copyright, 2000, Samuel R. Blackburn
$Workfile: CRandomNumberGenerator2.cpp $
$Modtime: 2/05/00 8:27a $