GClasses

GClasses::GRand Class Reference

This is a 64-bit pseudo-random number generator. More...

#include <GRand.h>

List of all members.

Public Member Functions

 GRand (uint64 seed)
 ~GRand ()
void setSeed (uint64 seed)
 Sets the seed.
uint64 next ()
 Returns an unsigned pseudo-random 64-bit value.
uint64 next (uint64 range)
 Returns a pseudo-random uint from a discrete uniform distribution in the range 0 to range-1 (inclusive). (This method guarantees the result will be drawn from a uniform distribution, whereas doing "next() % range" does not guarantee a truly uniform distribution.)
double beta (double alpha, double beta)
 Returns a random value from a beta distribution.
size_t binomial (size_t n, double p)
 Returns a random value from a binomial distribution This method draws n samples from a uniform distribution, so it is very slow for large values of n. binomial_approx is generally much faster.
size_t binomial_approx (size_t n, double p)
 Returns a random value approximately from a binomial distribution. This method uses a normal distribution to approximate the binomial distribution. It is O(1), and is generally quite accurate when n is large and p is not too close to 0 or 1.
size_t categorical (std::vector< double > &probabilities)
 Returns a random value from a categorical distribution with the specified vector of category probabilities.
double cauchy ()
 Returns a random value from a standard Cauchy distribution.
double chiSquare (double t)
 Returns a random value from a chi-squared distribution.
void dirichlet (double *pOutVec, const double *pParams, int dims)
 Returns a random vector from a dirichlet distribution with the specified parameters and dimensionality. (Both pOutVec and pParams should be an array of dims values.)
double exponential ()
 Returns a random value from a standard exponential distribution. (To convert it to a random value from an arbitrary exponential distribution, just divide the value this returns by the rate (usually lambda), or if you use the scale parameterization, just multiply the value this returns by the scale (usually beta)).
double f (double t, double u)
 Returns a random value from an f-distribution.
double gamma (double alpha)
 Returns a random value from a gamma distribution with beta=theta=1. To convert to a value from an arbitrary gamma distribution, just divide the value this returns by beta (or use alpha=k, and multiply the value this returns by theta).
int geometric (double p)
 Returns a random value from a geometric distribution with support for {0, 1, 2, ...}.
double logistic ()
 Returns a random value from a standard logistic distribution. To convert to a random value from an arbitrary logistic distribution, just multiply the value this returns by the scale (or divide by the steepness), and then add the mean.
double logNormal (double mean, double dev)
 Returns a random value from a log-Normal distribution.
double normal ()
 Returns a random value from a standard normal distribution. (To convert it to a random value from an arbitrary normal distribution, just multiply the value this returns by the deviation (usually lowercase-sigma), then add the mean (usually mu).)
int poisson (double mu)
 Returns a random value from a Poisson distribution.
void simplex (double *pOutVec, size_t dims)
 Draws uniformly from a unit simplex. (This is a special case of drawing from a dirichlet distribution with uniform parameters.)
double softImpulse (double s)
 Returns a random value from a soft-impulse distribution with support from 0 to 1. (The cdf of the soft-impulse distribution is the soft-step function: (1/(pow(1/x-1,s)+1)). The mean is always at 0.5, where the probability-density is s.
void spherical (double *pOutVec, size_t dims)
 Returns a random point on the surface of a dims-dimensional unit sphere.
void spherical_volume (double *pOutVec, size_t dims)
 Returns a random point within the volume of a dims-dimensional unit sphere.
void cubical (double *pOutVec, size_t dims)
 Returns a random point uniformly distributed within a unit cube.
double student (double t)
 Returns a random value from Student's t-distribution.
double uniform ()
 Returns a pseudo-random double from 0 (inclusive) to 1 (exclusive). This uses 52 random bits for the mantissa, and discards the extra 12 random bits.
double weibull (double gamma)
 Returns a random value from a Weibull distribution with lambda=1.

Static Public Member Functions

static GRandglobal ()
 Returns the global random number generator. Useful to avoid passing GRand* all over the place. Initializes the seed based on the time and some other system parameters the first time it is called.
static void test ()
 Performs unit tests for this class. Throws an exception if there is a failure.

Protected Attributes

uint64 m_a
uint64 m_b

Detailed Description

This is a 64-bit pseudo-random number generator.


Constructor & Destructor Documentation

GClasses::GRand::GRand ( uint64  seed)
GClasses::GRand::~GRand ( )

Member Function Documentation

double GClasses::GRand::beta ( double  alpha,
double  beta 
)

Returns a random value from a beta distribution.

size_t GClasses::GRand::binomial ( size_t  n,
double  p 
)

Returns a random value from a binomial distribution This method draws n samples from a uniform distribution, so it is very slow for large values of n. binomial_approx is generally much faster.

size_t GClasses::GRand::binomial_approx ( size_t  n,
double  p 
)

Returns a random value approximately from a binomial distribution. This method uses a normal distribution to approximate the binomial distribution. It is O(1), and is generally quite accurate when n is large and p is not too close to 0 or 1.

size_t GClasses::GRand::categorical ( std::vector< double > &  probabilities)

Returns a random value from a categorical distribution with the specified vector of category probabilities.

double GClasses::GRand::cauchy ( )

Returns a random value from a standard Cauchy distribution.

double GClasses::GRand::chiSquare ( double  t)

Returns a random value from a chi-squared distribution.

void GClasses::GRand::cubical ( double *  pOutVec,
size_t  dims 
)

Returns a random point uniformly distributed within a unit cube.

void GClasses::GRand::dirichlet ( double *  pOutVec,
const double *  pParams,
int  dims 
)

Returns a random vector from a dirichlet distribution with the specified parameters and dimensionality. (Both pOutVec and pParams should be an array of dims values.)

double GClasses::GRand::exponential ( )

Returns a random value from a standard exponential distribution. (To convert it to a random value from an arbitrary exponential distribution, just divide the value this returns by the rate (usually lambda), or if you use the scale parameterization, just multiply the value this returns by the scale (usually beta)).

double GClasses::GRand::f ( double  t,
double  u 
)

Returns a random value from an f-distribution.

double GClasses::GRand::gamma ( double  alpha)

Returns a random value from a gamma distribution with beta=theta=1. To convert to a value from an arbitrary gamma distribution, just divide the value this returns by beta (or use alpha=k, and multiply the value this returns by theta).

int GClasses::GRand::geometric ( double  p)

Returns a random value from a geometric distribution with support for {0, 1, 2, ...}.

static GRand& GClasses::GRand::global ( ) [static]

Returns the global random number generator. Useful to avoid passing GRand* all over the place. Initializes the seed based on the time and some other system parameters the first time it is called.

double GClasses::GRand::logistic ( )

Returns a random value from a standard logistic distribution. To convert to a random value from an arbitrary logistic distribution, just multiply the value this returns by the scale (or divide by the steepness), and then add the mean.

double GClasses::GRand::logNormal ( double  mean,
double  dev 
)

Returns a random value from a log-Normal distribution.

uint64 GClasses::GRand::next ( ) [inline]

Returns an unsigned pseudo-random 64-bit value.

uint64 GClasses::GRand::next ( uint64  range)

Returns a pseudo-random uint from a discrete uniform distribution in the range 0 to range-1 (inclusive). (This method guarantees the result will be drawn from a uniform distribution, whereas doing "next() % range" does not guarantee a truly uniform distribution.)

double GClasses::GRand::normal ( )

Returns a random value from a standard normal distribution. (To convert it to a random value from an arbitrary normal distribution, just multiply the value this returns by the deviation (usually lowercase-sigma), then add the mean (usually mu).)

int GClasses::GRand::poisson ( double  mu)

Returns a random value from a Poisson distribution.

void GClasses::GRand::setSeed ( uint64  seed)

Sets the seed.

void GClasses::GRand::simplex ( double *  pOutVec,
size_t  dims 
)

Draws uniformly from a unit simplex. (This is a special case of drawing from a dirichlet distribution with uniform parameters.)

double GClasses::GRand::softImpulse ( double  s)

Returns a random value from a soft-impulse distribution with support from 0 to 1. (The cdf of the soft-impulse distribution is the soft-step function: (1/(pow(1/x-1,s)+1)). The mean is always at 0.5, where the probability-density is s.

void GClasses::GRand::spherical ( double *  pOutVec,
size_t  dims 
)

Returns a random point on the surface of a dims-dimensional unit sphere.

void GClasses::GRand::spherical_volume ( double *  pOutVec,
size_t  dims 
)

Returns a random point within the volume of a dims-dimensional unit sphere.

double GClasses::GRand::student ( double  t)

Returns a random value from Student's t-distribution.

static void GClasses::GRand::test ( ) [static]

Performs unit tests for this class. Throws an exception if there is a failure.

double GClasses::GRand::uniform ( )

Returns a pseudo-random double from 0 (inclusive) to 1 (exclusive). This uses 52 random bits for the mantissa, and discards the extra 12 random bits.

double GClasses::GRand::weibull ( double  gamma)

Returns a random value from a Weibull distribution with lambda=1.


Member Data Documentation