Random Numbers

From TDN


Contents


Random Number Generators

All of the Torque Game Engine derivatives use the same two algorithm for generating random numbers. You have the code so you can look in engine/math/mRandom.cc/h.

Linear Congruential Method

A Linear Congruential Generator (LCG) is a simple but well known algorithm for random number generation.


"Fast, fairly good random numbers (better than using rand())."
Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201

R250 Algorithm

R250 is a higher quality RNG algorithm, that is comparable in speed and quality to the 'Mersenne Twister'. Torque's R250 algorithm appears to use the LCG implementation(above) to seed itself.


See also: R250 Algorithm


"Fast, very good random numbers"
Kirkpatrick, S., and E. Stoll, 1981; A Very Fast Shift-Register
Sequence Random Number Generator, Journal of Computational Physics,V. 40.
Maier, W.L., 1991;

A Fast Pseudo Random Number Generator,
Dr. Dobb´s Journal, May, pp. 152 - 157
Period = 2^249


Other Considerations

- "I would say that it would only be a problem if you were relying on a some kind of determinism based upon a seeded value although there may be other scenarios I´ve not thought of." - Melv.

Using the RNGs in Torque

(stub: Inro)

MRandomLCG

(stub: short tutorial and code samples of how to correctly used Torque's built in RNG's - needed here)


MRandomR250

(stub: short tutorial and code samples needed; I started it but could be expanded! ~SP)

Judging from other examples in the code the R250 may be the easier/better RNG to use. It's a two step process:

  1. Instantiate an R250 object,
  2. Generate a random number,

The MRandom250 is seeded and initlized for you by the constructor. If passed no arguments, it generates a low-quality random seed to initialize itself. You can optionally pass it a seed to your own liking. (Personally, i'm not sure which is preferable ~SP).

MRandomR250 mRandom1;      // Default Constructor called.
U32 x = mRandom1.randI();
U32 seed = Platform::getRealMilliseconds();
MRandomR250 mRandom2(seed);      // Use the seed I give you.

U32 x = mRandom2.randI();