DART  6.7.3
dart::math::Random Class Referencefinal

#include <Random.hpp>

Public Types

using GeneratorType = std::mt19937
 
template<typename FloatType >
using UniformRealDist = std::uniform_real_distribution< FloatType >
 
template<typename IntType >
using UniformIntDist = std::uniform_int_distribution< IntType >
 
template<typename FloatType >
using NormalRealDist = std::normal_distribution< FloatType >
 

Static Public Member Functions

static GeneratorTypegetGenerator ()
 Returns a mutable reference to the random generator. More...
 
static void setSeed (unsigned int seed)
 Sets the seed value. More...
 
static unsigned int generateSeed (bool applyGeneratedSeed=false)
 Generates a seed value using the default random device. More...
 
static unsigned int getSeed ()
 
template<typename S >
static S uniform (S min, S max)
 Returns a random number from an uniform distribution. More...
 
template<typename FixedSizeT >
static FixedSizeT uniform (typename FixedSizeT::Scalar min, typename FixedSizeT::Scalar max)
 Returns a random vector or matrix from an uniform distribution. More...
 
template<typename DynamicSizeVectorT >
static DynamicSizeVectorT uniform (int size, typename DynamicSizeVectorT::Scalar min, typename DynamicSizeVectorT::Scalar max)
 Returns a random vector from an uniform distribution. More...
 
template<typename DynamicSizeMatrixT >
static DynamicSizeMatrixT uniform (int rows, int cols, typename DynamicSizeMatrixT::Scalar min, typename DynamicSizeMatrixT::Scalar max)
 Returns a random matrix from an uniform distribution. More...
 
template<typename S >
static S normal (S mean, S sigma)
 Returns a random number from a normal distribution. More...
 

Static Private Member Functions

static unsigned int & getSeedMutable ()
 

Member Typedef Documentation

◆ GeneratorType

using dart::math::Random::GeneratorType = std::mt19937

◆ NormalRealDist

template<typename FloatType >
using dart::math::Random::NormalRealDist = std::normal_distribution<FloatType>

◆ UniformIntDist

template<typename IntType >
using dart::math::Random::UniformIntDist = std::uniform_int_distribution<IntType>

◆ UniformRealDist

template<typename FloatType >
using dart::math::Random::UniformRealDist = std::uniform_real_distribution<FloatType>

Member Function Documentation

◆ generateSeed()

unsigned int dart::math::Random::generateSeed ( bool  applyGeneratedSeed = false)
static

Generates a seed value using the default random device.

Parameters
[in]applyGeneratedSeedWhether to apply the generated seed.
Returns
The new seed value.

◆ getGenerator()

Random::GeneratorType & dart::math::Random::getGenerator ( )
static

Returns a mutable reference to the random generator.

◆ getSeed()

unsigned int dart::math::Random::getSeed ( )
static
Returns
The current seed value.

◆ getSeedMutable()

unsigned int & dart::math::Random::getSeedMutable ( )
staticprivate
Returns
A mutable reference to the seed.

◆ normal()

template<typename S >
S dart::math::Random::normal ( mean,
sigma 
)
static

Returns a random number from a normal distribution.

This template function can generate different scalar types of random numbers as:

  • Floating-point number: float, double, long double
  • Integer number: [unsigned] short, [unsigned] int, [unsigned] long, [unsigned] long long

Example:

// Generate a random int
int intVal = Random::normal(0, 10);
// Generate a random double
double dblVal = Random::normal(0.0, 10.0);
static S normal(S mean, S sigma)
Returns a random number from a normal distribution.
Definition: Random-impl.hpp:453
Parameters
[in]meanMean of the normal distribution.
[in]sigmaStandard deviation of the distribution.
See also
uniform()

◆ setSeed()

void dart::math::Random::setSeed ( unsigned int  seed)
static

Sets the seed value.

The same seed gives the same sequence of random values so that you can regenerate the same sequencial random values as long as you knot the seed value.

◆ uniform() [1/4]

template<typename DynamicSizeMatrixT >
DynamicSizeMatrixT dart::math::Random::uniform ( int  rows,
int  cols,
typename DynamicSizeMatrixT::Scalar  min,
typename DynamicSizeMatrixT::Scalar  max 
)
static

Returns a random matrix from an uniform distribution.

This variant is meant to be used for dynamic-size matrix.

Template Parameters
DynamicSizeMatrixTThe type of dynamic-size matrix.
Parameters
[in]rowsThe row size of the matrices.
[in]colsThe col size of the matrices.
[in]minThe constant value of the lower bound matrix.
[in]maxThe constant value of the upper bound matrix.
See also
uniform()

◆ uniform() [2/4]

template<typename DynamicSizeVectorT >
DynamicSizeVectorT dart::math::Random::uniform ( int  size,
typename DynamicSizeVectorT::Scalar  min,
typename DynamicSizeVectorT::Scalar  max 
)
static

Returns a random vector from an uniform distribution.

This variant is meant to be used for dynamic-size vector.

Example:

// Generate random matrices
Eigen::MatrixXi matXi = Random::uniform<Eigen::MatrixXi>(0, 10);
Eigen::MatrixXd matXd = Random::uniform<Eigen::MatrixXd>(0.0, 10.0);
Template Parameters
DynamicSizeVectorTThe type of dynamic-size vector.
Parameters
[in]sizeThe size of the vectors.
[in]minThe constant value of the lower bound vector.
[in]maxThe constant value of the upper bound vector.

◆ uniform() [3/4]

template<typename S >
S dart::math::Random::uniform ( min,
max 
)
static

Returns a random number from an uniform distribution.

This template function can generate different scalar types of random numbers as:

  • Floating-point number: float, double, long double
  • Integer number: [unsigned] short, [unsigned] int, [unsigned] long, [unsigned] long long

and vectors and matrices as:

  • Fixed-size: Eigen::Vector3i, Eigen::Vector3d, Eigen::Matrix4d, and so on.
  • Dynamic-size: Eigen::VectorXi, Eigen::VectorXd, Eigen::MatrixXd, and so on.

Example:

// Generate a random int in [0, 10]
int intVal1 = Random::uniform(0, 10);
int intVal2 = Random::uniform<int>(0, 10);
// Generate a random double in [0.0, 10.0)
double dblVal1 = Random::uniform(0.0, 10.0);
double dblVal2 = Random::uniform<double>(0, 10);
// Generate a random vector in [lb, ub)
Eigen::Vector3d lb = Eigen::Vector3d::Constant(1);
Eigen::Vector3d ub = Eigen::Vector3d::Constant(4);
Eigen::Vector3d vecVal1 = Random::uniform(lb, ub);
Eigen::Vector3d vecVal2 = Random::uniform<Eigen::Vector3d>(lb, ub);
// Generate a random matrix in [lb, ub)
Eigen::Matrix4f lb = Eigen::Matrix4f::Constant(1);
Eigen::Matrix4f ub = Eigen::Matrix4f::Constant(4);
Eigen::Matrix4f vecVal1 = Random::uniform(lb, ub);
Eigen::Matrix4f vecVal2 = Random::uniform<Eigen::Matrix4f>(lb, ub);
static S uniform(S min, S max)
Returns a random number from an uniform distribution.
Definition: Random-impl.hpp:412

Note that the end of the range is closed for integer types (i.e., [int_min, int_max]), but open for floating-point types (i.e., [float_min, float_max)).

Template Parameters
SThe type of random value.
Parameters
[in]minLower bound of the distribution.
[in]maxUpper bound of the distribution.
See also
normal()

◆ uniform() [4/4]

template<typename FixedSizeT >
FixedSizeT dart::math::Random::uniform ( typename FixedSizeT::Scalar  min,
typename FixedSizeT::Scalar  max 
)
static

Returns a random vector or matrix from an uniform distribution.

This is a helper function for the case that the each of lower and upper bound has an uniform element value in it. For example, the lower bound is [1, 1, 1] or [-2, -2].

This variant is meant to be used for fixed-size vector or matrix types. For dynamic-size types, please use other variants that takes the size of vector or matrix.

Example:

// Generate random vectors
Eigen::VectorXi vecXi = Random::uniform<Eigen::VectorXi>(0, 10);
Eigen::VectorXd vecXd = Random::uniform<Eigen::VectorXd>(0.0, 10.0);
Template Parameters
FixedSizeTThe type of fixed-size vector or fixed-size matrix.
Parameters
[in]minThe constant value of the lower bound.
[in]maxThe constant value of the upper bound.
See also
uniform()