A C++ Random Number Generator Class
About:
The code on this page provides a high-quality, fast, and easy-to-use C++
random number generator class that generates random variates for several
common distributions. If you are interested in distributions other than
what are included in this class, I recommend the random number generators
available in other projects such as the
Gnu Scientific Library
or in the C interface to the
R Project for Statistical Computing.
If you only need to work with the distributions listed below, you
may find this code useful.
The RNG class has member functions that generate random numbers
from the following distributions:
- Continuous: Uniform, Normal, Gamma, Exponential, Chi-Square, Beta
- Discrete: Binomial, Multinomial, Poisson
The algorithms are, to my knowledge, among the best available in terms
of both quality and speed.
Warning My original intention was for this class to support a single
instance of the random number generator class on a platform with 32 bit ints.
Some attempts have been made to support 64 bit ints and multiple instances
of the class, but they are not well tested. Considering I occasionally get bug
reports, there may still be some issues. In
addition, this class was not designed with multi-threaded applications in mind
and has few known issues (race conditions) that could cause problems with them.
Files and Documents:
- rng.h: The header file for the RNG class
- rng.C: The source file containing the non-inline
RNG member functions
- randtest.C: A simple C++ example that
illustrates how to use the RNG class
- rng.tar: A tar archive containing all of the
above files.
- Papers: A directory containing useful
information on algorithms for generating random variates on a computer.
- Source: A directory containing some source code
obtained from other sources.
Changes:
- Jun 22, 2008: Fixed reported bug in which non-static members of the class
were only initialized for the first instance of the class.
- May 15, 2005: Added multinomial generator.
- Jan 18, 2005: In the process of updating the code so that it works
on 64 bit machines. Uniform, normal, gamma and possibly other
generators work now on 64 bit machines.
- Nov 21, 2005: Changed Poisson generator to that used in R. Modified
Poisson and binomial generators to return ints rather than doubles.
- Nov 9, 2005: Changed binomial generator to that used in R.
- Aug 4, 2005: Added a constructor, minor optimizations.
- Jul 21, 2005: Minor optimizations. Vectors are now filled faster.
- Jun 18, 2005: Added functions that fill vectors with random numbers.
- Jan 27, 2005: Original posted version.
Return to my projects page.