yat  0.14.5pre
Public Member Functions | Static Public Member Functions | List of all members
theplu::yat::random::RNG Class Reference

Random Number Generator. More...

#include <yat/random/random.h>

Public Member Functions

unsigned long max (void) const
 Returns the largest number that the random number generator can return.
 
unsigned long min (void) const
 Returns the smallest number that the random number generator can return.
 
std::string name (void) const
 Returns the name of the random number generator.
 
const gsl_rng * rng (void) const
 
void seed (unsigned long s) const
 Set the seed s for the rng. More...
 
unsigned long seed_from_devurandom (void)
 Seed the rng using the /dev/urandom device. More...
 
int set_state (const RNG_state &)
 Set the state to state. More...
 

Static Public Member Functions

static RNGinstance (void)
 Get an instance of the Random Number Generator. More...
 

Detailed Description

Random Number Generator.

The RNG class is wrapper to the GSL random number generator (rng). In yat 0.8 (or older) this class provided a single global instance of the rng, and made sure there was only one point of access to the generator. Since version 0.9 this class provides one rng per thread in order to avoid collisions in multi-thread applications.

There are many different rng's available in GSL. RNG uses the default generator, unless the global variable gsl_rng_default has been modified (see GSL Manual). Note, gsl_rng_default should be changed before RNG creates its generator and safest way to achieve this is to modify gsl_rng_default prior calling instance() the first time.

There is information about how to change seeding and generators at run time without recompilation using environment variables in the GSL Manual. RNG supports seeding at compile time if you don't want to bother about environment variables and GSL.

The class provides one generator per thread. The first generator created is seeded with gsl_rng_default_seed and subsequent generators are seeded with gsl_rng_default_seed + 1, gsl_rng_default_seed + 2 etc, unless the seed has been modified with seed() or seed_from_devurandom().

See Also
Design Patterns (the singleton and adapter pattern). GSL documentation.

Member Function Documentation

static RNG* theplu::yat::random::RNG::instance ( void  )
static

Get an instance of the Random Number Generator.

Get an instance of RNG. If a random number generator is not already created for current thread, the call will create a new generator of type gsl_rng_default. If it is the first generator created it will be seeded with gsl_rng_default_seed; otherwise created generator will be seeded with seed + n, where seed is the latest seed set (with seed() or seed_from_devurandom()) The seed may be changed with the seed or seed_from_devurandom member functions.

Returns
A pointer to the random number generator.
See Also
seed and seed_from_devurandom
const gsl_rng* theplu::yat::random::RNG::rng ( void  ) const

Access underlying GSL random number generator speicific to current thread. Behaviour of returned generator is undefined outside current thread.

Returns
const pointer to underlying GSL random generator.
void theplu::yat::random::RNG::seed ( unsigned long  s) const

Set the seed s for the rng.

Set the seed s for the rng. If s is zero, a default value from the rng's original implementation is used (cf. GSL documentation).

This function will also effect generators created subsequently in other threads. The seed s is saved and subsequent generators will be created with seed s + 1, s + 2, etc.

See Also
seed_from_devurandom
unsigned long theplu::yat::random::RNG::seed_from_devurandom ( void  )

Seed the rng using the /dev/urandom device.

This function will also effect generators in other threads created subsequntly (see seed()).

Returns
The seed acquired from /dev/urandom.
int theplu::yat::random::RNG::set_state ( const RNG_state )

Set the state to state.

Returns
0 always.
Note
this function only effects the RNG in current thread
Exceptions
utility::GSL_erroron error
See Also
gsl_rng_memcpy

The documentation for this class was generated from the following file:

Generated on Tue Sep 26 2017 02:33:30 for yat by  doxygen 1.8.5