Support Forum       Library Source       SourceForge Page       G3D Web Page     
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
G3D::Random Class Reference

Random number generator. More...

Inherited by G3D::PrecomputedRandom.

Public Member Functions

 Random (uint32 seed=0xF018A4D2, bool threadsafe=true)
 
virtual ~Random ()
 
virtual uint32 bits ()
 Each bit is random.
 
virtual void cosHemi (float &x, float &y, float &z)
 Returns 3D unit vectors distributed according to a cosine distribution about the positive z-axis.
 
virtual void cosPowHemi (const float k, float &x, float &y, float &z)
 Returns 3D unit vectors distributed according to a cosine power distribution ( $ \cos^k \theta $) about the z-axis.
 
virtual void cosSphere (float &x, float &y, float &z)
 Returns 3D unit vectors distributed according to a cosine distribution about the z-axis.
 
virtual float gaussian (float mean, float stdev)
 Normally distributed reals.
 
virtual void hemi (float &x, float &y, float &z)
 Returns 3D unit vectors uniformly distributed on the hemisphere about the z-axis.
 
virtual int integer (int min, int max)
 Uniform random integer on the range [min, max].
 
virtual void reset (uint32 seed=0xF018A4D2, bool threadsafe=true)
 
virtual void sphere (float &x, float &y, float &z)
 Returns 3D unit vectors uniformly distributed on the sphere.
 
virtual float uniform (float low, float high)
 Uniform random float on the range [min, max].
 
virtual float uniform ()
 Uniform random float on the range [0, 1].
 

Static Public Member Functions

static Randomcommon ()
 A shared instance for when the performance and features but not consistency of the class are desired.
 

Protected Types

enum  {
  N = 624,
  M = 397,
  R = 31,
  U = 11,
  S = 7,
  T = 15,
  L = 18,
  A = 0x9908B0DF,
  B = 0x9D2C5680,
  C = 0xEFC60000
}
 Constants (important for the algorithm; do not modify) More...
 

Protected Member Functions

 Random (void *)
 For subclasses.
 
virtual void generate ()
 Generate the next N ints, and store them for readback later.
 

Protected Attributes

int index
 Index into state.
 
Spinlock lock
 Prevents multiple overlapping calls to generate().
 
bool m_threadsafe
 
uint32state
 State vector (these are the next N values that will be returned)
 

Detailed Description

Random number generator.

Threadsafe.

Useful for generating consistent random numbers across platforms and when multiple threads are involved.

Uses the Fast Mersenne Twister (FMT-19937) algorithm.

On average, uniform() runs about 2x-3x faster than rand().

Referenced Code:
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html

On OS X, Random is about 10x faster than drand48() (which is threadsafe) and 4x faster than rand() (which is not threadsafe).

See Also
Noise

Member Enumeration Documentation

anonymous enum
protected

Constants (important for the algorithm; do not modify)

Enumerator:
N 
M 
R 
U 
S 
T 
L 
A 
B 
C 

Constructor & Destructor Documentation

G3D::Random::Random ( void )
protected

For subclasses.

The void* parameter is just to distinguish this from the public constructor.

G3D::Random::Random ( uint32  seed = 0xF018A4D2,
bool  threadsafe = true 
)
Parameters
threadsafeSet to false if you know that this random will only be used on a single thread. This eliminates the lock and improves performance on some platforms.
virtual G3D::Random::~Random ( )
virtual

Member Function Documentation

virtual uint32 G3D::Random::bits ( )
virtual

Each bit is random.

Subclasses can choose to override just this method and the other methods will all work automatically.

Reimplemented in G3D::PrecomputedRandom.

Referenced by uniform().

static Random& G3D::Random::common ( )
static

A shared instance for when the performance and features but not consistency of the class are desired.

It is slightly (10%) faster to use a distinct instance than to use the common one.

Threadsafe.

virtual void G3D::Random::cosHemi ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors distributed according to a cosine distribution about the positive z-axis.

Reimplemented in G3D::PrecomputedRandom.

virtual void G3D::Random::cosPowHemi ( const float  k,
float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors distributed according to a cosine power distribution ( $ \cos^k \theta $) about the z-axis.

Reimplemented in G3D::PrecomputedRandom.

virtual void G3D::Random::cosSphere ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors distributed according to a cosine distribution about the z-axis.

virtual float G3D::Random::gaussian ( float  mean,
float  stdev 
)
virtual

Normally distributed reals.

virtual void G3D::Random::generate ( )
protectedvirtual

Generate the next N ints, and store them for readback later.

Called from bits()

virtual void G3D::Random::hemi ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors uniformly distributed on the hemisphere about the z-axis.

virtual int G3D::Random::integer ( int  min,
int  max 
)
virtual

Uniform random integer on the range [min, max].

virtual void G3D::Random::reset ( uint32  seed = 0xF018A4D2,
bool  threadsafe = true 
)
virtual
virtual void G3D::Random::sphere ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors uniformly distributed on the sphere.

Reimplemented in G3D::PrecomputedRandom.

virtual float G3D::Random::uniform ( float  low,
float  high 
)
inlinevirtual

Uniform random float on the range [min, max].

Reimplemented in G3D::PrecomputedRandom.

virtual float G3D::Random::uniform ( )
inlinevirtual

Uniform random float on the range [0, 1].

Reimplemented in G3D::PrecomputedRandom.

Member Data Documentation

int G3D::Random::index
protected

Index into state.

Spinlock G3D::Random::lock
protected

Prevents multiple overlapping calls to generate().

bool G3D::Random::m_threadsafe
protected
uint32* G3D::Random::state
protected

State vector (these are the next N values that will be returned)


documentation generated on Sat Jan 12 2013 22:49:56 using doxygen 1.8.2