Boost random number generator
Does anyone have a favorite boost random number generator and can you explain a little on how to implement it into code. I am trying to get the mersenne twister to work and was wondering if anyone had preference towards one of the others.
This code is adapted from the boost manual at http://www.boost.org/doc/libs/1_42_0/libs/random/index.html:
#include <iostream>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
using namespace std;
int main() {
typedef boost::mt19937 RNGType;
RNGType rng;
boost::uniform_int<> one_to_six( 1, 6 );
boost::variate_generator< RNGType, boost::uniform_int<> >
dice(rng, one_to_six);
for ( int i = 0; i < 6; i++ ) {
int n = dice();
cout << n << endl;
}
}
To explain the bits:
mt19937
is the mersenne twister generator,which generates the raw random numbers. A typedef is used here so you can easily change random number generator type.rng
is an instance of the twister generator.one_to_six
is an instance of a distribution. This specifies the numbers we want to generate and the distribution they follow. Here we want 1 to 6, distributed evenly.dice
is the thing that takes the raw numbers and the distribution, and creates for us the numbers we actually want.dice()
is a call tooperator()
for thedice
object, which gets the next random number following the distribution, simulating a random six-sided dice throw.
As it stands, this code produces the same sequence of dice throws each time. You can randomise the generator in its constructor:
RNGType rng( time(0) );
or by using its seed() member.
I found this link which gives a good overview of properties of different random number generators. I have copied the table from above link for convenience:
+-----------------------+-------------------+-----------------------------+------------------------+ | generator | length of cycle | approx. memory requirements | approx. relative speed | +-----------------------+-------------------+-----------------------------+------------------------+ | minstd_rand | 2^31-2 | sizeof(int32_t) | 40 | | rand48 | 2^48-1 | sizeof(uint64_t) | 80 | | lrand48 (C library) | 2^48-1 | - | 20 | | ecuyer1988 | approx. 2^61 | 2*sizeof(int32_t) | 20 | | kreutzer1986 | ? | 1368*sizeof(uint32_t) | 60 | | hellekalek1995 | 2^31-1 | sizeof(int32_t) | 3 | | mt11213b | 2^11213-1 | 352*sizeof(uint32_t) | 100 | | mt19937 | 2^19937-1 | 625*sizeof(uint32_t) | 100 | | lagged_fibonacci607 | approx. 2^32000 | 607*sizeof(double) | 150 | | lagged_fibonacci1279 | approx. 2^67000 | 1279*sizeof(double) | 150 | | lagged_fibonacci2281 | approx. 2^120000 | 2281*sizeof(double) | 150 | | lagged_fibonacci3217 | approx. 2^170000 | 3217*sizeof(double) | 150 | | lagged_fibonacci4423 | approx. 2^230000 | 4423*sizeof(double) | 150 | | lagged_fibonacci9689 | approx. 2^510000 | 9689*sizeof(double) | 150 | | lagged_fibonacci19937 | approx. 2^1050000 | 19937*sizeof(double) | 150 | | lagged_fibonacci23209 | approx. 2^1200000 | 23209*sizeof(double) | 140 | | lagged_fibonacci44497 | approx. 2^2300000 | 44497*sizeof(double) | 60 | +-----------------------+-------------------+-----------------------------+------------------------+
length of cycle: length of random number sequence before it starts repeating
There's no one-size-fits-all RNG. Sometimes statistical properties are important, sometimes cryptology, sometimes raw speed.