Thea

Threadsafe random number generator. More...
#include <Random.hpp>
Public Member Functions  
virtual uint32  bits () 
Get a sequence of random bits. More...  
virtual bool  coinToss () 
Toss a fair coin. More...  
virtual void  cosHemi (Real &x, Real &y, Real &z) 
Generate 3D unit vectors distributed according to a cosine distribution about the zaxis. More...  
virtual void  cosPowHemi (Real k, Real &x, Real &y, Real &z) 
Generate 3D unit vectors distributed according to a cosine power distribution ( ) about the zaxis. More...  
virtual Real  gaussian (Real mean, Real stddev) 
Generate normally distributed real numbers. More...  
virtual void  hemi (Real &x, Real &y, Real &z) 
Generate 3D unit vectors uniformly distributed on the hemisphere about the zaxis. More...  
virtual int32  integer () 
Uniform random integer in the range [0, MAX_INTEGER] (both endpoints inclusive). More...  
virtual int32  integer (int32 lo, int32 hi) 
Uniform random integer in the range [lo, hi] (both endpoints inclusive). More...  
virtual void  integers (int32 lo, int32 hi, int32 m, int32 *selected) 
Get m distinct random integers in [lo, hi] (endpoints inclusive). More...  
Random (uint32 seed=getRandomSeed(), bool threadsafe=true)  
Constructor. More...  
template<typename T >  
void  randomShuffle (int32 n, T *elems) 
Randomly shuffle a set of n elements. More...  
template<typename T >  
void  randomShuffle (int32 n, int32 k, T *elems) 
Get the first k elements of a random permutation of a set of n elements. More...  
virtual void  sortedIntegers (int32 lo, int32 hi, int32 m, int32 *selected) 
Get m distinct random integers in [lo, hi] (endpoints inclusive), sorted in ascending order. More...  
virtual void  sphere (Real &x, Real &y, Real &z) 
Generate 3D unit vectors uniformly distributed on the sphere. More...  
virtual Real  uniform (Real lo, Real hi) 
Uniform random real number in the range [lo, hi]. More...  
virtual Real  uniform01 () 
Uniform random real number in the range [0, 1]. More...  
virtual void  unitVector (intx n, Real *v) 
Generate nD unit vectors uniformly distributed on the (n  1)dimensional sphere. More...  
virtual  ~Random () 
Destructor. More...  
Static Public Member Functions  
static Random &  common () 
A shared, threadsafe random number generator. More...  
Static Public Attributes  
static int32 const  MAX_INTEGER = 0x7FFFFFFF 
The maximum signed integral value that can be generated by this class (similar to RAND_MAX). More...  
Protected Types 
Protected Member Functions  
virtual void  generate () 
Generate the next N ints, and store them for readback later. More...  
Random (void *)  
For subclasses. More...  
Static Protected Member Functions  
static uint32  getRandomSeed () 
Get a random number to seed the generator. More...  
Protected Attributes  
int32  index 
Index into state. More...  
Spinlock  lock 
Prevents multiple overlapping calls to generate(). More...  
bool  m_threadsafe 
Flag that can be set to false to deactivate locking and improve performance in singlethreaded scenarios. More...  
uint32 *  state 
State vector (these are the next N values that will be returned). More...  
Threadsafe random number generator.
Useful for generating consistent random numbers across platforms and when multiple threads are involved.
Uses the Fast Mersenne Twister (FMT19937) algorithm.
Derived from the G3D library: http://g3d.sourceforge.net
Definition at line 48 of file Random.hpp.

protected 
Constants (important for the algorithm; do not modify)
Definition at line 52 of file Random.hpp.

protected 
For subclasses.
The void * parameter is just to distinguish this from the public constructor.
Definition at line 43 of file Random.cpp.
Random  (  uint32  seed = getRandomSeed() , 
bool  threadsafe = true 

) 
Constructor.
seed  Use this to specify your own starting random seed if necessary. 
threadsafe  Set to false if you know that this random will only be used on a single thread. This eliminates the lock and could improve performance. 
Definition at line 49 of file Random.cpp.

virtual 
Destructor.
Definition at line 62 of file Random.cpp.

virtual 
Get a sequence of random bits.
Subclasses can choose to override just this method and the other methods will all work automatically.
Definition at line 69 of file Random.cpp.

virtual 
Toss a fair coin.
Definition at line 135 of file Random.cpp.

static 
A shared, threadsafe random number generator.
Suggested for general usage when a separate object is not required (e.g. as a replacement for std::rand()).
Definition at line 222 of file Random.hpp.

virtual 
Generate 3D unit vectors distributed according to a cosine distribution about the zaxis.
Definition at line 180 of file Random.cpp.

virtual 
Generate 3D unit vectors distributed according to a cosine power distribution ( ) about the zaxis.
Definition at line 204 of file Random.cpp.

virtual 
Generate normally distributed real numbers.
Definition at line 160 of file Random.cpp.

protectedvirtual 
Generate the next N ints, and store them for readback later.
Called from bits().
Definition at line 100 of file Random.cpp.

staticprotected 
Get a random number to seed the generator.
Definition at line 342 of file Random.cpp.

virtual 
Generate 3D unit vectors uniformly distributed on the hemisphere about the zaxis.
Definition at line 219 of file Random.cpp.

virtual 
Uniform random integer in the range [0, MAX_INTEGER] (both endpoints inclusive).
Similar to std::rand().
Definition at line 142 of file Random.cpp.

virtual 
Uniform random integer in the range [lo, hi] (both endpoints inclusive).
Negative arguments are ok.
Definition at line 148 of file Random.cpp.

virtual 
Get m distinct random integers in [lo, hi] (endpoints inclusive).
Slow if the range is very large, and possibly breaks if the upper limit of the range is very large (more than MAX_INTEGER / 2).
This function is similar to randomShuffle(int32, int32, T *), but trades speed for reduced memory usage (randomShuffle(int32, int32, T *) requires an array of size hi  lo + 1 – integers() requires essentially no extra memory).
From http://my.opera.com/subjam/blog/bookreviewprogrammingpearls . This implements Algorithm S in Section 3.4.2 of Knuth's Seminumerical Algorithms.
lo  Lower limit of range (inclusive). 
hi  Upper limit of range (inclusive). 
m  Number of distinct random integers requested from the range. 
selected  Used to return the generated random integers. Assumed to be preallocated to m elements. 
Definition at line 284 of file Random.cpp.
void randomShuffle  (  int32  n, 
T *  elems  
) 
Randomly shuffle a set of n elements.
This implements the FisherYates/Knuth shuffle.
Definition at line 194 of file Random.hpp.
void randomShuffle  (  int32  n, 
int32  k,  
T *  elems  
) 
Get the first k elements of a random permutation of a set of n elements.
This implements a partial FisherYates/Knuth shuffle.
n  The number of elements in the complete set. 
k  The size of the desired prefix of the random permutation. 
elems  The input set, also used to return the desired prefix of the random permutation. The function will set the first k entries of this array to be the computed prefix. The remaining n  k entries will contain the remainder of the elements, but not in random order. 
Definition at line 209 of file Random.hpp.

virtual 
Get m distinct random integers in [lo, hi] (endpoints inclusive), sorted in ascending order.
Slow if the range is very large, and possibly breaks if the upper limit of the range is very large (more than MAX_INTEGER / 2).
From http://my.opera.com/subjam/blog/bookreviewprogrammingpearls . This implements Algorithm S in Section 3.4.2 of Knuth's Seminumerical Algorithms.
lo  Lower limit of range (inclusive). 
hi  Upper limit of range (inclusive). 
m  Number of distinct random integers requested from the range. 
selected  Used to return the generated random integers in ascending order. Assumed to be preallocated to m elements. 
Definition at line 303 of file Random.cpp.

virtual 
Generate 3D unit vectors uniformly distributed on the sphere.
For the nD case, see unitVector().
Definition at line 226 of file Random.cpp.

virtual 
Uniform random real number in the range [lo, hi].
Negative arguments are ok.
Definition at line 119 of file Random.hpp.

virtual 
Uniform random real number in the range [0, 1].
Definition at line 126 of file Random.hpp.

virtual 
Generate nD unit vectors uniformly distributed on the (n  1)dimensional sphere.
n  The dimension of the latent space. 
v  Used to return the sampled unit vector. Must have at (at least) n allocated entries. 
Definition at line 253 of file Random.cpp.

protected 
Index into state.
Definition at line 73 of file Random.hpp.

protected 
Prevents multiple overlapping calls to generate().
Definition at line 67 of file Random.hpp.

protected 
Flag that can be set to false to deactivate locking and improve performance in singlethreaded scenarios.
Definition at line 76 of file Random.hpp.

static 
The maximum signed integral value that can be generated by this class (similar to RAND_MAX).
Definition at line 89 of file Random.hpp.

protected 
State vector (these are the next N values that will be returned).
Definition at line 70 of file Random.hpp.