The BeOS Random Number Library
... is a collection of pseudo-random number generators all of whom derive
from a common Abstract Base Class called RNG
#include "rng.h"
class RNG
{
static RNGInfo *info;
public:
virtual void Seed (uint32) = 0;
virtual uint32 lRand() = 0;
virtual double dRand();
virtual const RNGInfo *Info() { return info; };
} ;
All of the classes that derive from the base class provide a
Seed()
and an
lRand()
function (obviously!), but they also override
Info()
and provide a constructor
(which calls Seed) .
Some of them also override the dRand() function.
Member Function
Seed
This initialises the random generator (so is called by the constructor),
but it can also be called at any time to reset the generator.
(sort of obvious if you've used random number generators before).
lRand
This returns a random unsigned 32 bit integer in either
the range 0 - 2**31-1 or
the range 0 - 2**32-1 .
The range depends on what the original authors had coded.
dRand
This returns a random double precision floating point number in
either the range 0.0 <= n <= 1.0
or the range 0.0 <= n < 1.0
Again, what you get usually depends on what the origianl authors have coded.
If a derived class doesn't override this function, then you will get the base
class function which calls the virtual lRand() and divides the
result by a very large number.
Info
This returns a pointer to an RNGInfo structured data type
(defined in rng.h)
that describes the class,
an example is
static RNGInfoItem myitems[] =
{
{ "Class", "RNG"},
{ "Description", "Abstract Base Class for Random Number Generators" },
{ "Version", "1.0 : 1998-04-19" },
{ "Author", "John Cooke" },
{ "Email", "bebox@dircon.co.uk" }
} ;
#define NITEMS (sizeof(myitems)/sizeof(myitems[0]))
static RNGInfo myinfo[] = { NITEMS, myitems } ;
RNGInfo *RNG::info = myinfo;
The Generators
LCG
This is the "minimal standard" Linear Congruential Generator.
It is not very fast (it uses division/modulus operations)
and it is not very random, but it is better than a lot of random
number generators that come with OSes.
You should never use a generator that is less good than this one
(hence it's title - the minimal standard generator)
R250
A fast "shift-register" generator with a long period (the numbers don't
repeat for 2**250 cycles). And the code is simple to read.
This is my personal favourite.
ISAAC
See
here for more info on this one. It is very fast and (according to the
author) produces random numbers that are strong enough for encryption
purposes.
Mersenne Twister (MT)
This one is a monster!
It's period is so long that it would take millions of years
for the numbers to come round again.
Mother
George Marsaglia's The mother of all random number generators.
It has a period roughly the same as R250 but is four times slower!
And is a wimp compared to the Mersenne Twister.
Using the Library
The makefile provided in the distribution creates a static library,
you can convert it to a shared library if you know how (I don't!-).
Your code should only call Seed, lRand, etc
via (a pointer or reference to) the base class. See testrand.cpp for
an example. (This is C++ after all)
If you want to add more RNG's, then do so.
- create a header file for your class
- override Seed, lRand and Info
- write a constructor (to call Seed)
- include your header file in random.h
- create a C++ file with the member functions and static member initialisation.
- compile and put into the library