Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::Random< Generator_ > Class Template Referencefinal

A consolidated random number generator providing convenient APIs wrapping the C++ standard library <random>. More...

#include <random.h>

Public Types

using Generator = Generator_

Public Member Functions

 Random (uint32_t seed=std::random_device{}())
 Constructs a generator seeded with the given value.
template<typename T>
T uniform (const T &min, const T &max)
 Generates a random scalar/vector/matrix from the uniform distribution.
template<typename FixedSizeT>
FixedSizeT uniform (typename FixedSizeT::Scalar min, typename FixedSizeT::Scalar max)
 Generates a fixed-size vector/matrix from the uniform distribution with a single scalar range applied to every element.
template<typename DynamicVector>
DynamicVector uniform (Eigen::Index size, typename DynamicVector::Scalar min, typename DynamicVector::Scalar max)
 Generates a dynamic-size vector from the uniform distribution with a single scalar range applied to every element.
template<typename DynamicMatrix>
DynamicMatrix uniform (Eigen::Index rows, Eigen::Index cols, typename DynamicMatrix::Scalar min, typename DynamicMatrix::Scalar max)
 Generates a dynamic-size matrix from the uniform distribution with a single scalar range applied to every element.
template<typename T>
Quaternion< T > uniformQuaternion ()
 Generates a random unit quaternion uniformly distributed on SO(3) (Haar measure).
template<typename T>
Matrix3< T > uniformRotationMatrix ()
 Generates a random rotation matrix uniformly distributed on SO(3) (Haar measure).
template<typename T>
Isometry3< T > uniformIsometry3 (const Vector3< T > &min=Vector3< T >::Zero(), const Vector3< T > &max=Vector3< T >::Ones())
 Generates a random isometry (rigid transformation) on SE(3) with rotation drawn uniformly from SO(3) and translation drawn uniformly from the box [min, max].
template<typename T>
Affine3< T > uniformAffine3 (T scaleMin=T(0.1), T scaleMax=T(10.0), const Vector3< T > &min=Vector3< T >::Zero(), const Vector3< T > &max=Vector3< T >::Ones())
 Generates a random affine transformation with rotation drawn uniformly from SO(3), uniform scale drawn from [scaleMin, scaleMax], and translation drawn uniformly from the box [min, max].
template<typename T>
T normal (const T &mean, const T &sigma)
 Generates a value from a normal (Gaussian) distribution N(mean, sigma^2).
template<typename FixedSizeT>
FixedSizeT normal (typename FixedSizeT::Scalar mean, typename FixedSizeT::Scalar sigma)
 Generates a fixed-size vector/matrix with each element drawn independently from N(mean, sigma^2).
template<typename DynamicVector>
DynamicVector normal (Eigen::Index size, typename DynamicVector::Scalar mean, typename DynamicVector::Scalar sigma)
 Generates a dynamic-size vector with each element drawn independently from N(mean, sigma^2).
template<typename DynamicMatrix>
DynamicMatrix normal (Eigen::Index rows, Eigen::Index cols, typename DynamicMatrix::Scalar mean, typename DynamicMatrix::Scalar sigma)
 Generates a dynamic-size matrix with each element drawn independently from N(mean, sigma^2).
uint32_t getSeed () const
 Returns the seed last used to initialize the engine.
void setSeed (uint32_t seed)
 Re-seeds the internal engine, resetting its state to the deterministic sequence implied by seed.

Static Public Member Functions

static Random & GetSingleton ()
 Returns the process-wide singleton instance.

Detailed Description

template<typename Generator_ = std::mt19937>
class momentum::Random< Generator_ >

A consolidated random number generator providing convenient APIs wrapping the C++ standard library <random>.

The default engine is std::mt19937; any engine satisfying the C++ UniformRandomBitGenerator requirements can be substituted via the template parameter (see: https://en.cppreference.com/w/cpp/numeric/random).

Note
Reproducibility: given the same seed and the same Generator type, the sequence of engine outputs is deterministic. However, the values produced by uniform() / normal() are not guaranteed to be portable across platforms or standard library implementations because std::uniform_int_distribution, std::uniform_real_distribution, and std::normal_distribution are implementation-defined.
Warning
Thread safety: instances are not thread-safe. The internal generator holds mutable state that is mutated on every call. Concurrent use of the same instance (including the singleton returned by GetSingleton()) requires external synchronization.

Definition at line 33 of file random.h.

Member Typedef Documentation

◆ Generator

template<typename Generator_ = std::mt19937>
using momentum::Random< Generator_ >::Generator = Generator_

Definition at line 35 of file random.h.

Constructor & Destructor Documentation

◆ Random()

template<typename Generator>
momentum::Random< Generator >::Random ( uint32_t seed = std::random_device{}())
explicit

Constructs a generator seeded with the given value.

Parameters
seedSeed for the underlying engine. Defaults to a non-deterministic value drawn from std::random_device, which makes default-constructed instances non-reproducible.

Definition at line 192 of file random-inl.h.

Referenced by GetSingleton().

Member Function Documentation

◆ getSeed()

template<typename Generator>
uint32_t momentum::Random< Generator >::getSeed ( ) const
nodiscard

Returns the seed last used to initialize the engine.

Note
This reflects the value passed to the constructor or the most recent setSeed() call; it does not encode the engine's current state, so it cannot be used alone to resume a sequence after some samples have been drawn.

Definition at line 311 of file random-inl.h.

◆ GetSingleton()

template<typename Generator>
Random< Generator > & momentum::Random< Generator >::GetSingleton ( )
staticnodiscard

Returns the process-wide singleton instance.

Warning
The returned instance is shared across all callers and is not thread-safe. See class-level thread-safety note.

Definition at line 186 of file random-inl.h.

References Random().

Referenced by momentum::normal(), momentum::normal(), momentum::normal(), momentum::normal(), momentum::uniform(), momentum::uniform(), momentum::uniform(), momentum::uniform(), momentum::uniformAffine3(), momentum::uniformIsometry3(), and momentum::uniformQuaternion().

◆ normal() [1/4]

template<typename Generator>
template<typename T>
T momentum::Random< Generator >::normal ( const T & mean,
const T & sigma )
nodiscard

Generates a value from a normal (Gaussian) distribution N(mean, sigma^2).

Each element of a vector/matrix result is drawn independently.

Backed by std::normal_distribution, which requires a real (floating-point) scalar type.

Precondition
sigma >= 0.
Template Parameters
TScalar, fixed-size, or dynamic-size Eigen type with floating-point scalar.
Parameters
[in]meanDistribution mean (mu).
[in]sigmaDistribution standard deviation (sigma).

Definition at line 274 of file random-inl.h.

References momentum::detail::generateNormal().

◆ normal() [2/4]

template<typename Generator>
template<typename DynamicMatrix>
DynamicMatrix momentum::Random< Generator >::normal ( Eigen::Index rows,
Eigen::Index cols,
typename DynamicMatrix::Scalar mean,
typename DynamicMatrix::Scalar sigma )
nodiscard

Generates a dynamic-size matrix with each element drawn independently from N(mean, sigma^2).

Precondition
sigma >= 0, rows >= 0, and cols >= 0.
Template Parameters
DynamicMatrixDynamic-size Eigen matrix type with floating-point scalar.
Parameters
[in]rowsNumber of rows in the result.
[in]colsNumber of columns in the result.
[in]meanDistribution mean applied to every element.
[in]sigmaDistribution standard deviation applied to every element.

Definition at line 299 of file random-inl.h.

References momentum::detail::generateMatrixNormal().

◆ normal() [3/4]

template<typename Generator>
template<typename DynamicVector>
DynamicVector momentum::Random< Generator >::normal ( Eigen::Index size,
typename DynamicVector::Scalar mean,
typename DynamicVector::Scalar sigma )
nodiscard

Generates a dynamic-size vector with each element drawn independently from N(mean, sigma^2).

Precondition
sigma >= 0 and size >= 0.
Template Parameters
DynamicVectorDynamic-size Eigen vector type with floating-point scalar.
Parameters
[in]sizeNumber of elements in the result.
[in]meanDistribution mean applied to every element.
[in]sigmaDistribution standard deviation applied to every element.

Definition at line 289 of file random-inl.h.

References momentum::detail::generateMatrixNormal().

◆ normal() [4/4]

template<typename Generator>
template<typename FixedSizeT>
FixedSizeT momentum::Random< Generator >::normal ( typename FixedSizeT::Scalar mean,
typename FixedSizeT::Scalar sigma )
nodiscard

Generates a fixed-size vector/matrix with each element drawn independently from N(mean, sigma^2).

Precondition
sigma >= 0.
Template Parameters
FixedSizeTFixed-size Eigen vector/matrix type with floating-point scalar.
Parameters
[in]meanDistribution mean applied to every element.
[in]sigmaDistribution standard deviation applied to every element.

Definition at line 280 of file random-inl.h.

References momentum::detail::generateMatrixNormal().

◆ setSeed()

template<typename Generator>
void momentum::Random< Generator >::setSeed ( uint32_t seed)

Re-seeds the internal engine, resetting its state to the deterministic sequence implied by seed.

Definition at line 316 of file random-inl.h.

◆ uniform() [1/4]

template<typename Generator>
template<typename T>
T momentum::Random< Generator >::uniform ( const T & min,
const T & max )
nodiscard

Generates a random scalar/vector/matrix from the uniform distribution.

Supported element types are integer types accepted by std::uniform_int_distribution<T> (see: https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) and any type for which std::is_floating_point<T>::value is true (see: https://en.cppreference.com/w/cpp/types/is_floating_point). T may be a scalar, a fixed-size Eigen vector/matrix, or a dynamic-size Eigen vector/matrix (in which case the result has the same dimensions as min).

Precondition
min <= max element-wise.
For dynamic-size types, min and max must have matching dimensions.

The result lies in [min, max] for integer types and [min, max) for real types.

Warning
While the upper bound is mathematically exclusive for real types according to std::uniform_real_distribution, there are instances where the sampled value can equal the upper bound:
  • When built with -ffast-math, rounding can yield exactly max.
  • Some standard library implementations have known edge cases producing the upper bound.

For vector/matrix types, supplying vector/matrix bounds applies a per-element range; use the scalar-bounds overload to apply the same range to every element.

Random rand;
auto r0 = rand.uniform(0.0, 1.0); // random double in [0, 1)
auto r1 = rand.uniform(0.f, 1.f); // random float in [0, 1)
auto r3 = rand.uniform(0, 10); // random int in [0, 10]
auto r4 = rand.uniform(Vector2f(0, 1), Vector2f(2, 3)); // random Vector2f in ([0, 2], [1, 3))
Random(uint32_t seed=std::random_device{}())
Constructs a generator seeded with the given value.
Definition random-inl.h:192
T uniform(const T &min, const T &max)
Generates a random scalar/vector/matrix from the uniform distribution.
Definition random-inl.h:196
Vector2< float > Vector2f
Definition types.h:250
Template Parameters
TScalar, fixed-size, or dynamic-size Eigen type.
Parameters
[in]minInclusive lower bound (element-wise).
[in]maxUpper bound (inclusive for integer types, exclusive for real types).

Definition at line 196 of file random-inl.h.

References momentum::detail::generateUniform().

Referenced by uniformAffine3(), and uniformIsometry3().

◆ uniform() [2/4]

template<typename Generator>
template<typename DynamicMatrix>
DynamicMatrix momentum::Random< Generator >::uniform ( Eigen::Index rows,
Eigen::Index cols,
typename DynamicMatrix::Scalar min,
typename DynamicMatrix::Scalar max )
nodiscard

Generates a dynamic-size matrix from the uniform distribution with a single scalar range applied to every element.

Same range semantics as uniform(const T&, const T&).

Random rand;
auto r0 = rand.uniform<MatrixXf>(2, 3, 0, 1); // random MatrixXf of dimension 2x3 where each
element is in [0, 1)
MatrixX< float > MatrixXf
Definition types.h:82
Template Parameters
DynamicMatrixDynamic-size Eigen matrix type.
Parameters
[in]rowsNumber of rows in the result.
[in]colsNumber of columns in the result.
[in]minInclusive lower bound applied to every element.
[in]maxUpper bound applied to every element (inclusive for integer scalars, exclusive for real scalars).
Precondition
rows >= 0 and cols >= 0.

Definition at line 221 of file random-inl.h.

References momentum::detail::generateMatrixUniform().

◆ uniform() [3/4]

template<typename Generator>
template<typename DynamicVector>
DynamicVector momentum::Random< Generator >::uniform ( Eigen::Index size,
typename DynamicVector::Scalar min,
typename DynamicVector::Scalar max )
nodiscard

Generates a dynamic-size vector from the uniform distribution with a single scalar range applied to every element.

Same range semantics as uniform(const T&, const T&).

Random rand;
auto r0 = rand.uniform<VectorXf>(2, 0, 1); // random VectorXf of dimension 2 where each
element is in [0, 1)
VectorX< float > VectorXf
Definition types.h:209
Template Parameters
DynamicVectorDynamic-size Eigen vector type.
Parameters
[in]sizeNumber of elements in the result.
[in]minInclusive lower bound applied to every element.
[in]maxUpper bound applied to every element (inclusive for integer scalars, exclusive for real scalars).
Precondition
size >= 0.

Definition at line 211 of file random-inl.h.

References momentum::detail::generateMatrixUniform().

◆ uniform() [4/4]

template<typename Generator>
template<typename FixedSizeT>
FixedSizeT momentum::Random< Generator >::uniform ( typename FixedSizeT::Scalar min,
typename FixedSizeT::Scalar max )
nodiscard

Generates a fixed-size vector/matrix from the uniform distribution with a single scalar range applied to every element.

Same range semantics as uniform(const T&, const T&).

Random rand;
auto r0 = rand.uniform<Vector2f>(0, 1); // random Vector2f in ([0, 1), [0, 1))
Template Parameters
FixedSizeTFixed-size Eigen vector/matrix type.
Parameters
[in]minInclusive lower bound applied to every element.
[in]maxUpper bound applied to every element (inclusive for integer scalars, exclusive for real scalars).

Definition at line 202 of file random-inl.h.

References momentum::detail::generateMatrixUniform().

◆ uniformAffine3()

template<typename Generator>
template<typename T>
Affine3< T > momentum::Random< Generator >::uniformAffine3 ( T scaleMin = T(0.1),
T scaleMax = T(10.0),
const Vector3< T > & min = Vector3<T>::Zero(),
const Vector3< T > & max = Vector3<T>::Ones() )
nodiscard

Generates a random affine transformation with rotation drawn uniformly from SO(3), uniform scale drawn from [scaleMin, scaleMax], and translation drawn uniformly from the box [min, max].

Template Parameters
TFloating-point scalar type of the affine components (float or double). The defaults below are floating-point literals, so instantiation with an integer T is rejected by a static_assert in the implementation rather than silently truncating the defaults.
Parameters
scaleMinInclusive lower bound on the uniform scale factor.
scaleMaxInclusive upper bound on the uniform scale factor.
minInclusive lower bound on the translation component (per axis).
maxInclusive upper bound on the translation component (per axis).
Returns
A random Affine3<T>.

Definition at line 255 of file random-inl.h.

References uniform(), momentum::uniform(), and momentum::uniformRotationMatrix().

◆ uniformIsometry3()

template<typename Generator>
template<typename T>
Isometry3< T > momentum::Random< Generator >::uniformIsometry3 ( const Vector3< T > & min = Vector3<T>::Zero(),
const Vector3< T > & max = Vector3<T>::Ones() )
nodiscard

Generates a random isometry (rigid transformation) on SE(3) with rotation drawn uniformly from SO(3) and translation drawn uniformly from the box [min, max].

Template Parameters
TScalar type of the isometry components, typically float or double.
Parameters
minInclusive lower bound on the translation component (per axis).
maxInclusive upper bound on the translation component (per axis).
Returns
A random Isometry3<T>.

Definition at line 246 of file random-inl.h.

References uniform(), and momentum::uniformRotationMatrix().

◆ uniformQuaternion()

template<typename Generator>
template<typename T>
Quaternion< T > momentum::Random< Generator >::uniformQuaternion ( )
nodiscard

Generates a random unit quaternion uniformly distributed on SO(3) (Haar measure).

Definition at line 234 of file random-inl.h.

◆ uniformRotationMatrix()

template<typename Generator>
template<typename T>
Matrix3< T > momentum::Random< Generator >::uniformRotationMatrix ( )
nodiscard

Generates a random rotation matrix uniformly distributed on SO(3) (Haar measure).

Definition at line 240 of file random-inl.h.

References momentum::uniformQuaternion().


The documentation for this class was generated from the following files: