geomc 1.0
A c++ linear algebra template library
Classes | Functions
Random

Random number and object generation. More...

Classes

class  LCRand
 Implements a linear congruential pseudorandom number generator with period 264. More...
 
class  MTRand
 Mersenne twister pseudorandom number generator. More...
 
class  Random
 Abstraction for a source of random or pseudorandom bits. More...
 
class  Sampler< T >
 A class for randomly sampling a variety of regions over RN space. More...
 

Functions

RandomgetRandom ()
 
template<typename T >
void permute (T objs[], size_t len, Random &rng=*getRandom())
 
template<typename T >
void permute (std::vector< T > &objs, Random &rng=*getRandom())
 

Detailed Description

Random number and object generation.

To obtain random numbers, choose and construct a random number generator, then call rand() with the desired type as a template parameter:

Random *rng = new MTRand(mySeed);
float f = rng->rand<float>(); // a random number between 0 and 1.0 

Or use geom::getRandom() for a quick-and-dirty (not reentrant or threadsafe!) generator instance:

float f = getRandom()->rand<float>();

A note about random ranges

For integer random numbers in a range, it is far preferable to use the provided methods, rather than the "standard" rand() % rangeMax strategy, which is not a uniform distribution, most severely when rangeMax is a large fraction of RAND_MAX.

The provided methods of generating floating point numbers are also generally superior to the (again) "standard" method of calculating rand() / RAND_MAX, which excludes about 93% of representable numbers between 0 and 1, particularly in the regions near 0. geom::Random::rand(float) addresses this by carefully choosing the bits of the generated number explicitly. The method is fast, makes efficient use of the supplied entropy, and produces a uniform distribution. (Note that C++11's <random> library does not address this issue).

Function Documentation

◆ getRandom()

Random * getRandom ( )

Return a "default" convenience pseudorandom number generator, seeded with the system clock. This generator is the default generator shared among many functions and classes if no other generator is provided. Note that because it is shared and global, it (and any code that calls it) is not re-entrant or threadsafe. For thread-safe random number generation, construct your own per-thread Random objects and pass them to your sampling functions and objects.

◆ permute() [1/2]

void geom::permute ( std::vector< T > &  objs,
Random rng = *getRandom() 
)

Permute the elements of objs in-place.

Non re-entrant unless a safely-managed random number generator is supplied.

Parameters
objsA list of objects to be permuted.
rngA random number generator.

◆ permute() [2/2]

void geom::permute ( objs[],
size_t  len,
Random rng = *getRandom() 
)

Permute the elements of objs in-place.

Non re-entrant unless a safely-managed random number generator is supplied.

Parameters
objsA list of objects to be permuted.
lenCount of elements in objs.
rngA random number generator.