Class RandomNumberGenerator

Inheritance Relationships

Derived Types

Class Documentation

class RandomNumberGenerator

Abstraction layer for random number generation.

Subclasses of this type must define a seed setter and a function returning random double-precision numbers uniformly distributed between 0 and 1. Implementations should strive for consistent high performance over cryptographic unpredictability. Avoid depending on finite pools of entropy (such as /dev/random) as these will be quickly exhausted and cause filtering performance issues.

Namespace-level random functions, including s_rand(), navtk::experimental::rand() and navtk::experimental::rand_n(), are all implemented by calling methods on the current value of get_global_rng().

If you have an existing random number generator that conforms to the C++ RandomNumberEngine named requirements, you can use the RandomNumberEngineWrapper template class to convert it to RandomNumberGenerator.

Subclassed by navtk::experimental::LocalEngineWrapper, navtk::experimental::RandomNumberEngineWrapper< TEngine >

Public Functions

RandomNumberGenerator() = default
RandomNumberGenerator(const RandomNumberGenerator&) = delete

Copy and move are deleted to prevent object slicing.

Use std::shared_ptr.

RandomNumberGenerator(RandomNumberGenerator&&) = delete

Copy and move are deleted to prevent object slicing.

Use std::shared_ptr.

RandomNumberGenerator &operator=(const RandomNumberGenerator&) = delete

Copy and move are deleted to prevent object slicing.

Use std::shared_ptr.

RandomNumberGenerator &operator=(RandomNumberGenerator&&) = delete

Copy and move are deleted to prevent object slicing.

Use std::shared_ptr.

virtual ~RandomNumberGenerator() = default
virtual void seed(uint64_t seed) = 0

Reset the state of the underlying random number generation algorithm.

Parameters

seed – New seed value.

virtual double rand() = 0
Returns

A single random number from a uniform distribution between 0 and 1.

Vector rand(int num)
Parameters

num – Size of desired output Vector.

Returns

A Vector of the requested size populated with samples from a uniform distribution between 0 and 1, using calls to this->rand() to generate those values.

Matrix rand(int num_rows, int num_cols)
Parameters
  • num_rows – Row count of the output Matrix.

  • num_cols – Column count of the output Matrix.

Returns

A Matrix of the requested shape populated with samples from a uniform distribution between 0 and 1, using calls to this->rand() to generate those values.

virtual double rand_n()

Return a single random number from a normal (Gaussian) distribution with mean=0, sigma=1.

The default implementation uses this->rand() as a source of uniform randomness and transforms this to gaussian using the Marsaglia Polar Method. Subclasses may override this function if a different sampling algorithm is desired.

Returns

A single random number from a normal (Gaussian) distribution with mean=0, sigma=1.

Vector rand_n(int num)
Parameters

num – Size of the output Vector

Returns

A Vector of the requested size populated with samples from a normal (Gaussian) distribution with mean=0, sigma=1, using calls to this->rand_n() to generate those values.

Matrix rand_n(int num_rows, int num_cols)
Parameters
  • num_rows – Row count of the output Matrix.

  • num_cols – Column count of the output Matrix.

Returns

A Matrix of the requested shape populated with samples from a normal (Gaussian) distribution with mean=0, sigma=1, using calls to this->rand_n() to generate those values.