Robowflex  v0.1
Making MoveIt Easy
robowflex::RNG Namespace Reference

Collection of methods relating to random sampling. More...

Functions

void setSeed (unsigned int seed)
 Set the random seed of the underlying generator. More...
 
double uniform01 ()
 Generate a random real in [0,1). More...
 
double uniformReal (double lower_bound, double upper_bound)
 Generate a random real within given bounds: [lower_bound, upper_bound) More...
 
int uniformInt (int lower_bound, int upper_bound)
 Generate a random integer within given bounds: [lower_bound, upper_bound) More...
 
bool uniformBool ()
 Generate a random boolean. More...
 
double gaussian01 ()
 
double gaussian (double mean, double stddev)
 Generate a random real using a normal distribution with given mean and standard deviation. More...
 
double gaussian (double stddev)
 Generate a random real using a normal distribution with zero mean and given standard deviation. More...
 
Eigen::Vector3d uniformRPY (const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
 Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubound computed value has the order (roll, pitch, yaw). More...
 
Eigen::Vector3d uniformRPY (const Eigen::Vector3d &bounds)
 Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubound computed value has the order (roll, pitch, yaw). More...
 
Eigen::Vector3d unifromRPY ()
 Uniform random sampling of Euler roll-pitch-yaw angles, roll, yaw in range [-pi, pi) and pitch in range[-pi/2, pi/2) computed value has the order (roll, pitch, yaw). More...
 
Eigen::Vector3d uniformVec (const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
 Generate a uniform real vector within given bounds: [lower_bound, upper_bound) More...
 
Eigen::Vector3d uniformVec (const Eigen::Vector3d &bounds)
 Generate a uniform real vector within given bounds: [-bounds, bounds) More...
 
Eigen::Vector3d gaussianVec (const Eigen::Vector3d &mean, const Eigen::Vector3d &stddev)
 Generate a random real vector using a normal distribution with given mean and standard deviation. More...
 
Eigen::Vector3d gaussianVec (const Eigen::Vector3d &stddev)
 Generate a random real vector using a normal distribution with mean zero and standard deviation. More...
 
template<typename Iter >
Iter uniformSample (Iter start, Iter end)
 Choose a random element between start and end. More...
 
template<typename Type >
TypeuniformSample (std::vector< Type > &vector)
 Choose a random element from a vector. More...
 

Detailed Description

Collection of methods relating to random sampling.

Function Documentation

◆ gaussian() [1/2]

double robowflex::RNG::gaussian ( double  mean,
double  stddev 
)

Generate a random real using a normal distribution with given mean and standard deviation.

Parameters
[in]meanMean of the normal distribution.
[in]stddevStandard deviation of the normal distribution.
Returns
Sampled number.

Definition at line 47 of file random.cpp.

48 {
49  return gaussian01() * stddev + mean;
50 }
double gaussian01()
Definition: random.cpp:42

◆ gaussian() [2/2]

double robowflex::RNG::gaussian ( double  stddev)

Generate a random real using a normal distribution with zero mean and given standard deviation.

Parameters
[in]stddevStandard deviation of the normal distribution.
Returns
Sampled number.

Definition at line 52 of file random.cpp.

53 {
54  return gaussian01() * stddev;
55 }

◆ gaussian01()

double robowflex::RNG::gaussian01 ( )

Generate a random real using a normal distribution with mean 0 and variance 1

Returns
Sampled number.

Definition at line 42 of file random.cpp.

43 {
44  return NORMALDIST(GENERATOR);
45 }

◆ gaussianVec() [1/2]

Eigen::Vector3d robowflex::RNG::gaussianVec ( const Eigen::Vector3d &  mean,
const Eigen::Vector3d &  stddev 
)

Generate a random real vector using a normal distribution with given mean and standard deviation.

Parameters
[in]meanMean vector of the normal distribution.
[in]stddevStandard deviation vector (diagonal covariance) of the normal distribution.
Returns
Sampled vector.

Definition at line 103 of file random.cpp.

104 {
105  Eigen::Vector3d vec;
106  vec[0] = gaussian(mean[0], stddev[0]);
107  vec[1] = gaussian(mean[1], stddev[1]);
108  vec[2] = gaussian(mean[2], stddev[2]);
109  return vec;
110 }
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and standard deviation.
Definition: random.cpp:47

◆ gaussianVec() [2/2]

Eigen::Vector3d robowflex::RNG::gaussianVec ( const Eigen::Vector3d &  stddev)

Generate a random real vector using a normal distribution with mean zero and standard deviation.

Parameters
[in]stddevStandard deviation vector (diagonal covariance) of the normal distribution.
Returns
Sampled vector.

Definition at line 112 of file random.cpp.

113 {
114  Eigen::Vector3d vec;
115  vec[0] = gaussian(stddev[0]);
116  vec[1] = gaussian(stddev[1]);
117  vec[2] = gaussian(stddev[2]);
118 
119  return vec;
120 }

◆ setSeed()

void robowflex::RNG::setSeed ( unsigned int  seed)

Set the random seed of the underlying generator.

Parameters
[in]seedSeed to set in generator.

Definition at line 15 of file random.cpp.

16 {
17  GENERATOR.seed(seed);
18 }

◆ uniform01()

double robowflex::RNG::uniform01 ( )

Generate a random real in [0,1).

Returns
Sampled number.

Definition at line 20 of file random.cpp.

21 {
22  return UNIDIST(GENERATOR);
23 }

◆ uniformBool()

bool robowflex::RNG::uniformBool ( )

Generate a random boolean.

Returns
Sampled number.

Definition at line 37 of file random.cpp.

38 {
39  return uniform01() <= 0.5;
40 }
double uniform01()
Generate a random real in [0,1).
Definition: random.cpp:20

◆ uniformInt()

int robowflex::RNG::uniformInt ( int  lower_bound,
int  upper_bound 
)

Generate a random integer within given bounds: [lower_bound, upper_bound)

Parameters
[in]lower_boundLower bound of uniform distribution.
[in]upper_boundUpper bound of uniform distribution.
Returns
Sampled number.

Definition at line 31 of file random.cpp.

32 {
33  auto r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound)));
34  return (r > upper_bound - 1) ? upper_bound - 1 : r;
35 }
T floor(T... args)
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:25
T upper_bound(T... args)

◆ uniformReal()

double robowflex::RNG::uniformReal ( double  lower_bound,
double  upper_bound 
)

Generate a random real within given bounds: [lower_bound, upper_bound)

Parameters
[in]lower_boundLower bound of uniform distribution.
[in]upper_boundUpper bound of uniform distribution.
Returns
Sampled number.

Definition at line 25 of file random.cpp.

26 {
27  assert(lower_bound <= upper_bound);
28  return (upper_bound - lower_bound) * uniform01() + lower_bound;
29 }
T lower_bound(T... args)

◆ uniformRPY() [1/2]

Eigen::Vector3d robowflex::RNG::uniformRPY ( const Eigen::Vector3d &  bounds)

Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubound computed value has the order (roll, pitch, yaw).

Parameters
[in]bounds[-bounds, bounds] is lower and upper bound is respectively.
Returns
roll-pitch-yaw vector.

Definition at line 77 of file random.cpp.

78 {
79  return uniformRPY(-bounds, bounds);
80 }
Eigen::Vector3d uniformRPY(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubou...
Definition: random.cpp:57

◆ uniformRPY() [2/2]

Eigen::Vector3d robowflex::RNG::uniformRPY ( const Eigen::Vector3d &  lbound,
const Eigen::Vector3d &  ubound 
)

Uniform random sampling of Euler roll-pitch-yaw angles within lower bound lbound and upper bound ubound computed value has the order (roll, pitch, yaw).

Parameters
[in]lboundLower bound for roll pitch yaw.
[in]uboundUpper bound for roll pitch yaw.
Returns
roll-pitch-yaw vector.

Definition at line 57 of file random.cpp.

58 {
59  const double phi_min = std::max(-constants::pi, lbound[0]);
60  const double chi_min = std::max(-constants::half_pi, lbound[1]);
61  const double psi_min = std::max(-constants::pi, lbound[2]);
62 
63  const double phi_max = std::min(constants::pi, ubound[0]);
64  const double chi_max = std::min(constants::half_pi, ubound[1]);
65  const double psi_max = std::min(constants::pi, ubound[2]);
66 
67  Eigen::Vector3d v;
68  // From Effective Sampling and Distance Metrics for 3D Rigid Body Path Planning, 2004
69  v[0] = uniformReal(phi_min, phi_max);
70  v[1] = acos(uniformReal(cos(chi_min + constants::half_pi), cos(chi_max + constants::half_pi))) -
72  v[2] = uniformReal(psi_min, psi_max);
73 
74  return v;
75 }
T acos(T... args)
T cos(T... args)
T max(T... args)
T min(T... args)
static const double half_pi
Definition: constants.h:22
static const double pi
Definition: constants.h:21

◆ uniformSample() [1/2]

template<typename Iter >
Iter robowflex::RNG::uniformSample ( Iter  start,
Iter  end 
)

Choose a random element between start and end.

Parameters
[in]startStart iterator.
[in]endEnd iterator.
Returns
Chosen element.

Definition at line 121 of file random.h.

122  {
123  std::advance(start, uniformInt(0, std::distance(start, end)));
124  return start;
125  };
T advance(T... args)
T distance(T... args)
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:31

◆ uniformSample() [2/2]

template<typename Type >
Type& robowflex::RNG::uniformSample ( std::vector< Type > &  vector)

Choose a random element from a vector.

Parameters
[in]vectorVector to sample from.
Returns
Chosen element.

Definition at line 132 of file random.h.

133  {
134  return *uniformSample(vector.begin(), vector.end());
135  }
T begin(T... args)
T end(T... args)
Type & uniformSample(std::vector< Type > &vector)
Choose a random element from a vector.
Definition: random.h:132

◆ uniformVec() [1/2]

Eigen::Vector3d robowflex::RNG::uniformVec ( const Eigen::Vector3d &  bounds)

Generate a uniform real vector within given bounds: [-bounds, bounds)

Parameters
[in]boundsUpper and (negative) lower bound vector of uniform distribution.
Returns
Sampled vector.

Definition at line 98 of file random.cpp.

99 {
100  return uniformVec(-bounds, bounds);
101 }
Eigen::Vector3d uniformVec(const Eigen::Vector3d &lbound, const Eigen::Vector3d &ubound)
Generate a uniform real vector within given bounds: [lower_bound, upper_bound)
Definition: random.cpp:88

◆ uniformVec() [2/2]

Eigen::Vector3d robowflex::RNG::uniformVec ( const Eigen::Vector3d &  lbound,
const Eigen::Vector3d &  ubound 
)

Generate a uniform real vector within given bounds: [lower_bound, upper_bound)

Parameters
[in]lboundLower bound vector of uniform distribution.
[in]uboundUpper bound vector of uniform distribution.
Returns
Sampled vector.

Definition at line 88 of file random.cpp.

89 {
90  Eigen::Vector3d vec;
91  vec[0] = uniformReal(lbound[0], ubound[0]);
92  vec[1] = uniformReal(lbound[1], ubound[1]);
93  vec[2] = uniformReal(lbound[2], ubound[2]);
94 
95  return vec;
96 }

◆ unifromRPY()

Eigen::Vector3d robowflex::RNG::unifromRPY ( )

Uniform random sampling of Euler roll-pitch-yaw angles, roll, yaw in range [-pi, pi) and pitch in range[-pi/2, pi/2) computed value has the order (roll, pitch, yaw).

Returns
roll-pitch-yaw vector.

Definition at line 82 of file random.cpp.

83 {
84  return uniformRPY(Eigen::Vector3d{-constants::pi, -constants::half_pi, -constants::pi},
86 }