MyRNG

A convenient random number generator

View the Project on GitHub BioND/myrng

MyRNG: A convenient random number generator

The MyRNG C++ classes provide two very efficient random number generators and several methods for generating random variates. You can choose between the Mersenne Twister MT19937a algorithm in the implementation of Makoto Matsumoto and Takuji Nishimura and the well-equidistributed long-period linear generator WELL1024a implemented by Marton Morvai. Both generators are essentially shuffled linear congruential random number generators; they should not be used for cryptography applications but should otherwise cover most scientific needs. While the WELL1024a is slightly faster in the current implementation, the MT19937a has an astronomical recurrence time of 2^19937. Furthermore, the methods are provided to generate several random variates including uniform, Gaussian, Beta, and Gamma distributions. Generation of most random variates is based on Law and Kelton, 2000. Generation of the Gamma distribution is based on Marsagli and Tsang, 2000.

Getting started

MyRNG can be used as a shared or header-only library. To use it as a shared library include either myrngMT.h (MT19937a) or myrngWELL.h (WELL1024a) in your code, and link the compiled binaries to libmyrngMT or libmyrngWELL, respectively. Including the header files creates an instance of the corresponding myrng::RandomVariates class called rng. Random variates can be obtained by calling the corresponding member functions.

For instance, a random variable from a uniform distribution between 0 and 1 can be obtained as

#include <myrngWELL.h>      // use the WELL1024a generator
double u = rng.Uniform01();

and a random variable from an exponential distribution with mean m as

#include <myrngMT.h>       // let's use the MT19937 generator
double m = 0.7;
double v = rng.Exponential(m);

The preferred method to use MyRNG is to create your own instance of the random number generator instead of using a global variable. To that end, do not include the above header files. Instead, use for this purpose

#include <myrng.h>
myrng::WELL1024a rng_well; // random variates using the WELL1024a generator
myrng::MT19937 rng_mt;     // random variates using the MT19937 generator
double x = rng_well.Exponential(0.3);
double y = rng_mt.Exponential(0.7);

In this case, you do not have to link the compiled binaries to additional libraries.

For further information see the full source code documentation, which you can extract using the doxygen source code documentation tool.

Disclaimer

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.