Humble Framework for SkyOS


Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

HRandom Class Reference
[Abstract Data Types]

#include <HRandom.h>

Inheritance diagram for HRandom:

HObjNoCopy HObj

Detailed Description

The HRandom class implements a random number generator that uses the Mersenne Twister algorithm, which generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting from any odd seed in 0..(2^32 - 1).

It is "designed with consideration of the flaws of various existing generators," has a period of 2^19937 - 1, gives a sequence that is 623-dimensionally equidistributed, and "has passed many stringent tests, including the die-hard test of G. Marsaglia and the load test of P. Hellekalek and S. Wegenkittl." It is efficient in memory usage (typically using 2506 to 5012 bytes of static data) and the code is quite short as well). It generates random numbers in batches of 624 at a time, so the caching and pipelining of modern systems is exploited. It is also divide- and mod-free.

We initialize m_uState[0..(N-1)] via the generator

    x_new = (69069 * x_old) mod 2^32

from Line 15 of Table 1, p. 106, Sec. 3.3.4 of Knuth Volume 2, 3rd ed. The generator's potency (min. s>=0 with (69069-1)^s = 0 mod 2^32) is 16, which seems to be alright by p. 25, Sec. 3.2.1.3 of Knuth. It also does well in the dimension 2..5 spectral tests, but it could be better in dimension 6 (Line 15, Table 1, p. 106, Sec. 3.3.4, Knuth).

Note that the random number user does not see the values generated here directly since reload() will always munge them first, so maybe none of all of this matters. In fact, the seed values made here could even be extra-special desirable if the Mersenne Twister theory says so--that's why the only change made is to restrict to odd seeds.

Definition at line 57 of file HRandom.h.

Public Member Functions

 HRandom (uint32 uSeed=0)
uint32 Get (void)
 Generates a random 32-bit value.
uint32 Get (uint32 uMax)
 Generate a random 32-bit value within a specific range.
int32 Get (uint32 uCount, uint32 uDieSize, int32 nBonus=0)
 Generate a random 32-bit value within a specific range.
uint32 GetSeed (void) const
 Returns the seed value used to seed the random number generator.
ErrCode SetSeed (uint32 uSeed)
 Seeds the random number generator.

Protected Member Functions

uint32 reload (void)
 Computes the next set of random values.

Protected Attributes

int32 m_nLeft
 Values remaining before reloading.
uint32 m_uSeed
 Original seed value.
uint32 * m_puNext
 Ptr to next random value.
uint32 m_uState [kLength+1]
 State vector.


Member Function Documentation

uint32 HRandom::reload void   )  [protected]
 

Returns:
Next random value

Definition at line 93 of file HRandom.h.

uint32 HRandom::Get void   ) 
 

Returns:
Random 32-bit value

Definition at line 138 of file HRandom.h.

uint32 HRandom::Get uint32  uMax  ) 
 

Returns:
Random 32-bit value [0 <= ? < uMax]
Parameters:
uMax Maximum value

Definition at line 161 of file HRandom.h.

int32 HRandom::Get uint32  uCount,
uint32  uDieSize,
int32  nBonus = 0
 

This version of Get() is specifically designed to make "dice rolling" easier. For example, calling Get(4, 8, +1) would be the same as rolling four 8-sided dice, adding 1 to each roll, and summing the result; this would be "4d8+1" in FRP parlance. Note that unlike the other versions of Get(), this method can return a negative value if, and only if, the bonus/penalty parameter is negative.

Returns:
Random 32-bit value [0 <= ? <= uMax]
Parameters:
uCount Number of rolls
uDieSize Maximum value
nBonus [optional] Per roll bonus/penalty

Definition at line 187 of file HRandom.h.

uint32 HRandom::GetSeed void   )  const
 

Returns:
Seed value

Definition at line 207 of file HRandom.h.

ErrCode HRandom::SetSeed uint32  uSeed  ) 
 

Returns:
Always NO_ERROR;
Parameters:
uSeed Seed value (any non-zero integer)

Definition at line 215 of file HRandom.h.


The documentation for this class was generated from the following file:
 

2006.01.09-16:37