random.h

This file defines the basic pseudorandom number generation functionality used by the core library, as well as functions to sample from a number of built-in distributions such as categorical, discrete uniform, continuous uniform, Bernoulli, geometric, beta, gamma, and Dirichlet.

Classes, functions, and variables in this file
unsigned intget_seed ()
voidset_seed (unsigned int new_seed)
boolread_random_state (Stream & in)
boolwrite_random_state (Stream & out)
unsigned intsample_categorical (V * probability, unsigned int length)
unsigned intselect_categorical (const U * probability, V random, unsigned int length)
unsigned intsample_categorical (const V * probability, V sum, unsigned int length)
unsigned intsample_categorical (const unsigned int * probability, unsigned int sum, unsigned int length)
unsigned intsample_uniform (unsigned int n)
const T &sample_uniform (const T * elements, unsigned int length)
const T &sample_uniform (const T(&) elements[N])
const T &sample_uniform (const array< T > & elements)
Vsample_uniform ()
boolsample_bernoulli (const V & p)
unsigned intsample_geometric (const V & p)
doublelog_probability_negative_binomial (unsigned int x, const V & r, const V & p)
Vsample_beta (const V & alpha)
Vsample_beta (const V & alpha, const V & beta)
Vsample_gamma (const V & alpha, const V & beta)
Vlog_probability_gamma (const V & x, const V & alpha, const V & beta)
voidsample_dirichlet (V * dst, const V * alpha, unsigned int length)

Returns the initial random seed used by all core functions that require pseudorandom number generation.

void set_seed(
unsigned intnew_seed)

Sets the seed of the underlying pseudorandom number generator.

template<typename Stream>
bool read_random_state(
Stream &in)

Reads the state of the pseudorandom number generator from in. This is useful to persist the state of the pseudorandom number generator.

template<typename Stream>
bool write_random_state(
Stream &out)

Writes the state of the pseudorandom number generator to out. This is useful to persist the state of the pseudorandom number generator.

template<typename V>
unsigned int sample_categorical(
V *probability,
unsigned intlength)

Samples from a categorical distribution, where the unnormalized probability of returning the index i is given by probability[i]. This function normalizes and overwrites probability with its cumulative distribution function.

template<typename U, typename V>
unsigned int select_categorical(
const U *probability,
Vrandom,
unsigned intlength)

Returns the smallest index i such that random < sum from j=0 to i-1 of probability[j]. Thus, this function implements the inverse cumulative distribution function for the categorical distribution with unnormalized probabilities given by probability.

template<typename V>
unsigned int sample_categorical(
const V *probability,
Vsum,
unsigned intlength)

Samples from a categorical distribution, where the unnormalized probability of returning the index i is given by probability[i] and its sum is given by the floating-point sum. This function doesn't modify probability.

unsigned int sample_categorical(
const unsigned int *probability,
unsigned intsum,
unsigned intlength)

Samples from a categorical distribution, where the unnormalized probability of returning the index i is given by probability[i] and its sum is given by the unsigned integer sum. This function doesn't modify probability.

unsigned int sample_uniform(
unsigned intn)

Returns a sample from the discrete uniform distribution over {0, ..., n - 1}.

template<typename T>
const T & sample_uniform(
const T *elements,
unsigned intlength)

Returns a sample from the discrete uniform distribution over elements.

template<typename T, size_t N>
const T & sample_uniform(
const T(&)elements[N])

Returns a sample from the discrete uniform distribution over elements.

template<typename T>
const T & sample_uniform(
const array< T > &elements)

Returns a sample from the discrete uniform distribution over elements.

template<typename V>
V sample_uniform()

Returns a sample from the continuous uniform distribution over [0, 1].

template<typename V>
bool sample_bernoulli(
const V &p)

Returns a sample from the Bernoulli distribution: with probability 0.5, true is returned, otherwise false is returned.

template<typename V>
unsigned int sample_geometric(
const V &p)

Returns a sample from the geometric distribution with success probability p.

template<typename V>
double log_probability_negative_binomial(
unsigned intx,
const V &r,
const V &p)

Returns a sample from the negative binomial distribution with number of failures r and success probability p.

template<typename V>
V sample_beta(
const V &alpha)

Returns a sample from the beta distribution with shape parameter alpha and scale parameter 1. This function assumes alpha > 0.

template<typename V>
V sample_beta(
const V &alpha,
const V &beta)

Returns a sample from the beta distribution with shape parameter alpha and scale parameter beta. This function assumes alpha > 0 and beta > 0.

template<typename V>
V sample_gamma(
const V &alpha,
const V &beta)

Returns a sample from the gamma distribution with shape parameter alpha and rate parameter beta. This function assumes alpha > 0 and beta > 0.

template<typename V>
V log_probability_gamma(
const V &x,
const V &alpha,
const V &beta)

Returns the log probability of drawing the observation x from a gamma distribution with shape parameter alpha and rate parameter beta. This function assumes x > 0, alpha > 0, and beta > 0.

template<typename V>
void sample_dirichlet(
V *dst,
const V *alpha,
unsigned intlength)

Samples from the Dirichlet distribution with parameter alpha and dimension length. The sample is written to dst. This function assumes dst has size at least length, alpha[i] > 0 for all i = 0, ..., length - 1.