Support Forum       G3D Web Page     
Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
G3D::PrecomputedRandom Class Reference

Fast random numbers using a precomputed data table. More...

Inherits G3D::Random.

Classes

class  HemiUniformData
 Put the cosHemi and the uniform together so that when alternating between them we stay in cache. More...
 
class  SphereBitsData
 

Public Member Functions

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

Static Public Member Functions

static Randomcommon ()
 A shared instance for when the performance and features but not consistency of the class are desired. More...
 
static RandomthreadCommon ()
 Returns a non-threadsafe Random instance initialized with a thread-ID based seed for the current thread. More...
 

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

virtual void generate ()
 Generate the next N ints, and store them for readback later. More...
 

Protected Attributes

int index
 Index into state. More...
 
Spinlock lock
 Prevents multiple overlapping calls to generate(). More...
 
bool m_freeData
 If true, free m_hemiUniform and m_sphereBits in destructor. More...
 
const HemiUniformDatam_hemiUniform
 Array of 2^n elements. More...
 
int m_index
 
int m_modMask
 2^n - 1; the AND mask for computing a fast modulo More...
 
const SphereBitsDatam_sphereBits
 
bool m_threadsafe
 
uint32state
 State vector (these are the next N values that will be returned) More...
 

Detailed Description

Fast random numbers using a precomputed data table.

e.g., generates cosHemi about 13x faster than Random. This is useful for quickly generating seeded random numbers for reproducibility. G3D::Random takes a long time to seed; this is instantaneous (providing the precomputed data is already available.)

Not threadsafe.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protectedinherited

Constants (important for the algorithm; do not modify)

Enumerator

Constructor & Destructor Documentation

◆ PrecomputedRandom() [1/2]

G3D::PrecomputedRandom::PrecomputedRandom ( const HemiUniformData data1,
const SphereBitsData data2,
int  dataSize,
uint32  seed = 0xF018A4D2 
)

◆ PrecomputedRandom() [2/2]

G3D::PrecomputedRandom::PrecomputedRandom ( int  dataSize,
uint32  seed = 0xF018A4D2 
)
Parameters
dataSizeNumber of random numbers that can be requested before periodicity. Must be a power of 2.

◆ ~PrecomputedRandom()

G3D::PrecomputedRandom::~PrecomputedRandom ( )

Member Function Documentation

◆ bits()

virtual uint32 G3D::PrecomputedRandom::bits ( )
overridevirtual

Each bit is random.

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

Reimplemented from G3D::Random.

◆ common()

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

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.

Referenced by G3D::Array< G3D::AABox >::randomElement(), and G3D::Array< G3D::AABox >::randomize().

◆ cosHemi()

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

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

Reimplemented from G3D::Random.

◆ cosPowHemi()

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

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

Reimplemented from G3D::Random.

◆ cosSphere()

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

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

◆ gaussian()

virtual float G3D::Random::gaussian ( float  mean,
float  variance 
)
virtualinherited

Normally distributed reals.

◆ generate()

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

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

Called from bits()

◆ hemi()

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

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

◆ integer()

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

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

Referenced by G3D::Array< G3D::AABox >::randomElement(), and G3D::Array< G3D::AABox >::randomize().

◆ reset()

virtual void G3D::PrecomputedRandom::reset ( uint32  seed = 0xF018A4D2,
bool  threadsafe = true 
)
overridevirtual

Reimplemented from G3D::Random.

◆ sphere()

virtual void G3D::PrecomputedRandom::sphere ( float &  x,
float &  y,
float &  z 
)
overridevirtual

Returns 3D unit vectors uniformly distributed on the sphere.

Reimplemented from G3D::Random.

◆ threadCommon()

static Random& G3D::Random::threadCommon ( )
staticinherited

Returns a non-threadsafe Random instance initialized with a thread-ID based seed for the current thread.

This will always return the same instance for the same thread. Calling this repeatedly with too many threads will consume resources.

Useful for efficiently and safely producing random numbers with Thread::runConcurrently.

◆ uniform() [1/2]

virtual float G3D::PrecomputedRandom::uniform ( float  low,
float  high 
)
overridevirtual

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

Reimplemented from G3D::Random.

◆ uniform() [2/2]

virtual float G3D::PrecomputedRandom::uniform ( )
overridevirtual

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

Reimplemented from G3D::Random.

Member Data Documentation

◆ index

int G3D::Random::index
protectedinherited

Index into state.

◆ lock

Spinlock G3D::Random::lock
protectedinherited

Prevents multiple overlapping calls to generate().

◆ m_freeData

bool G3D::PrecomputedRandom::m_freeData
protected

If true, free m_hemiUniform and m_sphereBits in destructor.

◆ m_hemiUniform

const HemiUniformData* G3D::PrecomputedRandom::m_hemiUniform
protected

Array of 2^n elements.

◆ m_index

int G3D::PrecomputedRandom::m_index
protected

◆ m_modMask

int G3D::PrecomputedRandom::m_modMask
protected

2^n - 1; the AND mask for computing a fast modulo

◆ m_sphereBits

const SphereBitsData* G3D::PrecomputedRandom::m_sphereBits
protected

◆ m_threadsafe

bool G3D::Random::m_threadsafe
protectedinherited

◆ state

uint32* G3D::Random::state
protectedinherited

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


documentation generated on Wed Nov 24 2021 08:01:59 using doxygen 1.8.15