by Agner Fog, 2001 - 2007
randomc.zip contains a C++ class library of uniform random number generators of good quality.
The random number generators found in standard libraries are often of a poor quality, insufficient for large Monte Carlo calculations. This C++ implementation provides random number generators of a much better quality: Better randomness, higher resolution, and longer cycle lengths.
The same random number generators are available as libraries coded in assembly language for higher speed. These libraries can be linked into projects coded in other programming languages under Windows, Linux, BSD, etc. The library files are available in the archive asmlib.zip.
Non-uniform random number generators are provided in stocc.zip.
The archive randomc.zip contains the following files:
#include
this in all C++ files that use this library.All these random number generators provide excellent randomness and extremely long cycle lengths.
For all but the most demanding applications it doesn't matter which of the random number generators you use. The Mersenne twister is the one that is best understood theoretically. For this reason it is recommended by leading experts. The Mother-of-all generator has the highest bifurcation of these generators.
For the most demanding scientific applications you may combine these two generators, using rancombi.cpp.
Choose which one of the random number generators mentioned above you want to use.
Write #include "randomc.h"
in any C++ file
that uses one of these random number generators.
Add the appropriate cpp file to your project, either as an #include
or as a separate module.
Make an instance (object) of the appropriate class. It needs an integer seed as initializer.
The seed can be any integer, positive, negative or zero. Repeating the calculations with the same seed will produce the same sequence of random numbers. A different seed will give different random numbers. You may use the time in seconds or milliseconds as seed.
The random number generators that are implemented as C++ classes (CRandomMersenne, CRandomMother, CRandomMersenneA, CRandomMotherA) are thread-safe if you have one instance of the class for each thread that needs random numbers. Make sure each instance has a different seed. You may, for example, add 1 to the seed for each new instance. The versions that are not wrapped in classes are not thread-safe and should not be used in multithreaded programs.
The file ex-ran.cpp contains an example of how to use the random number generators. Try it!
The C++ class library is supposed to work with all C++ compilers and all operating systems. It has been tested on several different systems.
There are, however, a few system differences that you need to be aware of:
The theory of the Mersenne twister is given in the article:
M. Matsumoto & T. Nishimura: "Mersenne Twister: A 623-Dimensionally
Equidistributed Uniform Pseudo-Random Number Generator". ACM
Transactions on Modeling and Computer Simulation, vol. 8, no. 1, 1998, pp. 3-30.
See also http://www.math.sci.hiroshima-u.ac.jp/~m-mat/eindex.html.
The theory of Mother-of-All generators is given in George Marsaglia's DIEHARD package, see stat.fsu.edu/~geo/diehard.html or www.cs.hku.hk/internet/randomCD.html.
© 2002-2007 by Agner Fog. General public license statement.