ImFusion SDK 4.3
Generator Class Reference

#include <ImFusion/Core/Random.h>

Convenience class for generating random numbers from various distributions. More...

Detailed Description

Convenience class for generating random numbers from various distributions.

Auto-seeded by default (with a relatively high quality seed), but can be explicitly seeded for reproducibility. Uses the Pcg64Engine class as the underlying random engine. The implicit copy-constructor and copy-assignment operators copy the engine's state. This means the outputs of copies are the same as that of the copied.

See also
https://www.pcg-random.org/

Public Types

using Seed = Pcg64Engine::result_type
 

Public Member Functions

 Generator ()=default
 Default constructor, auto-seeds the engine.
 
 Generator (Seed seed)
 Constructor with a low quality seed (use for tests if you need reproducibility).
 
void reseed ()
 Re-seeds the engine with an automatically generated seed.
 
void reseed (Seed seed)
 Re-seeds the engine with a low quality seed.
 
template<typename T>
getUniformInteger (T lower, T upper)
 Returns a uniformly random integer between lower and upper, both included.
 
template<typename T>
getUniformReal (T lower, T upper)
 Returns a uniformly random floating-point number between lower and upper, upper excluded.
 
template<typename T>
getNormal (T mean, T sigma)
 Returns a normally-distributed random number with given mean and sigma.
 
template<typename T>
getBeta (T alpha, T beta)
 Returns a value distributed according to the Beta distribution with positive parameters alpha and beta.
 
template<typename T>
pick (std::initializer_list< T > list)
 Picks one item at random in the list (all items have equal chances to be picked).
 
template<typename InputIt>
auto pick (InputIt begin, InputIt end) -> decltype(*begin)
 Picks one item at random within the range (all items have equal chances to be picked).
 
template<typename Range>
auto pick (Range &&range) -> decltype(*std::begin(range))
 Picks one item at random within the range (all items have equal chances to be picked).
 
template<typename InputIt>
InputIt choose (InputIt begin, InputIt end)
 Chooses one iterator at random within the range (similar to pick; returns the iterator instead of the item).
 
template<typename Range>
auto choose (Range &&range) -> decltype(std::begin(range))
 Chooses one iterator at random within the range (similar to pick; returns the iterator instead of the item).
 
template<typename T, template< typename > class Distribution, typename... Params>
getVariate (Params &&... params)
 Returns a random number from a given distribution and with given parameters.
 
template<typename InputIt, typename T>
void fillUniformInteger (InputIt begin, InputIt end, T lower, T upper)
 Fills given range with uniformly-distributed random integers between lower and upper, both included.
 
template<typename Range, typename T>
void fillUniformInteger (Range &&range, T lower, T upper)
 Fills given range with uniformly-distributed random integers between lower and upper, both included.
 
template<typename InputIt, typename T>
void fillUniformReal (InputIt begin, InputIt end, T lower, T upper)
 Fills given range with uniformly-distributed random floating-point numbers, between lower and upper.
 
template<typename Range, typename T>
void fillUniformReal (Range &&range, T lower, T upper)
 Fills given range with uniformly-distributed random floating-point numbers between lower and upper.
 
template<typename InputIt, typename T>
void fillNormal (InputIt begin, InputIt end, T mean, T sigma)
 Fills given range with normally-distributed random numbers with given mean and sigma.
 
template<typename Range, typename T>
void fillNormal (Range &&range, T mean, T sigma)
 Fills given range with normally-distributed random numbers with given mean and sigma.
 
template<typename InputIt, typename T>
void fillBeta (InputIt begin, InputIt end, T mean, T sigma)
 Fills given range with Beta-distributed random numbers with Beta distribution parameters alpha and beta.
 
template<typename Range, typename T>
void fillBeta (Range &&range, T alpha, T beta)
 Fills given range with Beta-distributed random numbers with Beta distribution parameters alpha and beta.
 
template<template< typename > class Distribution, typename InputIt, typename... Params>
void fillVariate (InputIt begin, InputIt end, Params &&... params)
 Fills given range with random numbers from a given distribution and with given parameters.
 
template<template< typename > class Distribution, typename Range, typename... Params>
void fillVariate (Range &&range, Params &&... params)
 Fills given range with random numbers from a given distribution and with given parameters.
 
template<typename InputIt>
void shuffle (InputIt begin, InputIt end)
 Shuffles a range. All items within the range have equal chances to end up at each position.
 
template<typename Range>
void shuffle (Range &&range)
 Shuffles a range.
 
template<typename T>
std::vector< T > pickN (std::size_t n, std::initializer_list< T > list)
 Picks n different items at random in the list and returns them as a vector (all items have equal chances to be picked).
 
template<typename InputIt>
auto pickN (std::size_t n, InputIt begin, InputIt end) -> std::vector< detail::RemoveCVRef< decltype(*begin)> >
 Picks n different items at random within the range and returns them as a vector (all items have equal chances to be picked).
 
template<typename Range>
auto pickN (std::size_t n, Range &&range) -> std::vector< detail::RemoveCVRef< decltype(*std::begin(range))> >
 Picks n different items at random within the range (all items have equal chances to be picked).
 
template<typename InputIt>
InputIt partition (std::size_t size, InputIt begin, InputIt end)
 Samples size items from the range.
 
template<typename Range>
auto partition (std::size_t size, Range &&range) -> decltype(std::begin(range))
 Samples size items from the range.
 
vec3 getUnitVector ()
 Generates a random unit vector (uniformly distributed on the sphere).
 
mat3 getRotationMatrix ()
 Generates a random rotation matrix.
 

Member Function Documentation

◆ reseed()

void reseed ( Seed seed)

Re-seeds the engine with a low quality seed.

Note that instead of using the same seed multiple times for reproducible processes, you may want to keep a default-constructed main generator instance and copy-construct a new instance to be used in the reproducible process. Copy-construction and copy-assignment guarantee the same output for the copy and the original.

◆ getUniformInteger()

template<typename T>
T getUniformInteger ( T lower,
T upper )

Returns a uniformly random integer between lower and upper, both included.

If lower coincides with upper, this value is returned.

Note
This template member function is implemented for the following types T:
  • uint8_t and int8_t
  • uint16_t and int16_t
  • uint32_t and int32_t
  • uint64_t and int64_t
See also
https://www.boost.org/doc/libs/1_80_0/doc/html/boost/random/uniform_int_distribution.html

◆ getUniformReal()

template<typename T>
T getUniformReal ( T lower,
T upper )

Returns a uniformly random floating-point number between lower and upper, upper excluded.

Lower needs to be strictly smaller than upper. If needed, use previousReal() or nextReal() to include or exclude upper (or lower - or both).

Note
This template member function is implemented for the following types T:
  • float
  • double
See also
https://www.boost.org/doc/libs/1_80_0/doc/html/boost/random/uniform_real_distribution.html

◆ getNormal()

template<typename T>
T getNormal ( T mean,
T sigma )

Returns a normally-distributed random number with given mean and sigma.

Instantiated for types float and double.

◆ getBeta()

template<typename T>
T getBeta ( T alpha,
T beta )

Returns a value distributed according to the Beta distribution with positive parameters alpha and beta.

If alpha and beta are 1, a uniform distribution U(0,1) is used. Instantiated for types float and double.

See also
https://en.wikipedia.org/wiki/Beta_distribution

◆ pick()

template<typename Range>
auto pick ( Range && range) -> decltype(*std::begin(range))

Picks one item at random within the range (all items have equal chances to be picked).

range must work with std::begin and std::end.

◆ choose()

template<typename Range>
auto choose ( Range && range) -> decltype(std::begin(range))

Chooses one iterator at random within the range (similar to pick; returns the iterator instead of the item).

range must work with std::begin and std::end.

◆ getVariate()

template<typename T, template< typename > class Distribution, typename... Params>
T getVariate ( Params &&... params)

Returns a random number from a given distribution and with given parameters.

params is forwarded to the distribution's constructor. For example, this will return a random integer according to the binomial distribution with 5 trials, each with a 70% chance of success: generator.getVariate<int, std::binomial_distribution>(5, 0.7);

◆ fillUniformInteger() [1/2]

template<typename InputIt, typename T>
void fillUniformInteger ( InputIt begin,
InputIt end,
T lower,
T upper )

Fills given range with uniformly-distributed random integers between lower and upper, both included.

lower <= upper is required. The produced values are uniformly distributed before being fed into the iterator, caller is responsible for making sure values are not changed at this point (by e.g. overflow). For a fixed seed the result does not depend on the type of iterator.

◆ fillUniformInteger() [2/2]

template<typename Range, typename T>
void fillUniformInteger ( Range && range,
T lower,
T upper )

Fills given range with uniformly-distributed random integers between lower and upper, both included.

range must work with std::begin and std::end.

◆ fillUniformReal() [1/2]

template<typename InputIt, typename T>
void fillUniformReal ( InputIt begin,
InputIt end,
T lower,
T upper )

Fills given range with uniformly-distributed random floating-point numbers, between lower and upper.

Upper may or may not be included (see getUniformReal).

◆ fillUniformReal() [2/2]

template<typename Range, typename T>
void fillUniformReal ( Range && range,
T lower,
T upper )

Fills given range with uniformly-distributed random floating-point numbers between lower and upper.

Upper may or may not be included (see getUniformReal). range must work with std::begin and std::end.

◆ fillVariate() [1/2]

template<template< typename > class Distribution, typename InputIt, typename... Params>
void fillVariate ( InputIt begin,
InputIt end,
Params &&... params )

Fills given range with random numbers from a given distribution and with given parameters.

params is forwarded to the distribution's constructor. For example, this will generate random values into the range (from begin to end), according to the gamma distribution with parameters alpha = 1 and beta = 2.3: generator.generate<std::gamma_distribution>(begin, end, 1, 2.3); Note that the distribution functions in std are not platform independent.

◆ fillVariate() [2/2]

template<template< typename > class Distribution, typename Range, typename... Params>
void fillVariate ( Range && range,
Params &&... params )

Fills given range with random numbers from a given distribution and with given parameters.

range must work with std::begin and std::end. params is forwarded to the distribution's constructor. For example, this will generate random values into values (a floating-point container), according to the normal distribution with a mean of 6 and a standard deviation of 2.3: generator.generate<std::normal_distribution>(values, 6, 2.3); Note that the distribution functions in std are not platform independent.

◆ shuffle()

template<typename Range>
void shuffle ( Range && range)

Shuffles a range.

All items within the range have equal chances to end up at each position. range must work with std::begin and std::end.

◆ pickN()

template<typename Range>
auto pickN ( std::size_t n,
Range && range ) -> std::vector<detail::RemoveCVRef<decltype(*std::begin(range))>>

Picks n different items at random within the range (all items have equal chances to be picked).

range must work with std::begin and std::end.

◆ partition() [1/2]

template<typename InputIt>
InputIt partition ( std::size_t size,
InputIt begin,
InputIt end )

Samples size items from the range.

Returns and iterator to the first element that wasn't sampled. In other words, picks size different items at random within the range and places them at the beginning, partitionning the range (all items have equal chances to be picked).

◆ partition() [2/2]

template<typename Range>
auto partition ( std::size_t size,
Range && range ) -> decltype(std::begin(range))

Samples size items from the range.

Returns and iterator to the first element that wasn't sampled. In other words, picks size different items at random within the range and places them at the beginning, making a partition (all items have equal chances to be picked). range must work with std::begin and std::end.

◆ getRotationMatrix()

mat3 getRotationMatrix ( )

Generates a random rotation matrix.

Equivalent to three orthogonal unit vectors (all uniformly distributed on the sphere).


The documentation for this class was generated from the following files:
Search Tab / S to search, Esc to close