RandomNumbers.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan, Jonathan Gammell */
36 
37 #ifndef OMPL_UTIL_RANDOM_NUMBERS_
38 #define OMPL_UTIL_RANDOM_NUMBERS_
39 
40 #include <memory>
41 #include <random>
42 #include <cassert>
43 #include <cstdint>
44 
45 #include "ompl/config.h"
46 #if OMPL_HAVE_EIGEN3
47 #include "ompl/util/ProlateHyperspheroid.h"
48 #endif
49 
50 namespace ompl
51 {
58  class RNG
59  {
60  public:
62  RNG();
63 
65  RNG(std::uint_fast32_t localSeed);
66 
68  double uniform01()
69  {
70  return uniDist_(generator_);
71  }
72 
74  double uniformReal(double lower_bound, double upper_bound)
75  {
76  assert(lower_bound <= upper_bound);
77  return (upper_bound - lower_bound) * uniDist_(generator_) + lower_bound;
78  }
79 
81  int uniformInt(int lower_bound, int upper_bound)
82  {
83  auto r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound) + 1.0));
84  return (r > upper_bound) ? upper_bound : r;
85  }
86 
88  bool uniformBool()
89  {
90  return uniDist_(generator_) <= 0.5;
91  }
92 
94  double gaussian01()
95  {
96  return normalDist_(generator_);
97  }
98 
100  double gaussian(double mean, double stddev)
101  {
102  return normalDist_(generator_) * stddev + mean;
103  }
104 
111  double halfNormalReal(double r_min, double r_max, double focus = 3.0);
112 
116  int halfNormalInt(int r_min, int r_max, double focus = 3.0);
117 
120  void quaternion(double value[4]);
121 
124  void eulerRPY(double value[3]);
125 
128  static void setSeed(std::uint_fast32_t seed);
129 
133  static std::uint_fast32_t getSeed();
134 
137  void setLocalSeed(std::uint_fast32_t localSeed);
138 
143  std::uint_fast32_t getLocalSeed() const
144  {
145  return localSeed_;
146  }
147 
150  void uniformNormalVector(unsigned int n, double value[]);
151 
155  void uniformInBall(double r, unsigned int n, double value[]);
156 
157 #if OMPL_HAVE_EIGEN3
158 
167  void uniformProlateHyperspheroidSurface(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr,
168  double value[]);
169 
179  void uniformProlateHyperspheroid(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr, double value[]);
180 #endif
181 
182  private:
185  class SphericalData;
186 
188  std::uint_fast32_t localSeed_;
189  std::mt19937 generator_;
190  std::uniform_real_distribution<> uniDist_{0,1};
191  std::normal_distribution<> normalDist_{0,1};
192  // A structure holding boost::uniform_on_sphere distributions and the associated boost::variate_generators for
193  // various dimension
194  std::shared_ptr<SphericalData> sphericalDataPtr_;
195  };
196 }
197 
198 #endif
double gaussian01()
Generate a random real using a normal distribution with mean 0 and variance 1.
Definition: RandomNumbers.h:94
bool uniformBool()
Generate a random boolean.
Definition: RandomNumbers.h:88
void quaternion(double value[4])
Uniform random unit quaternion sampling. The computed value has the order (x,y,z,w). The return variable value is expected to already exist.
RNG()
Constructor. Always sets a different random seed.
void eulerRPY(double value[3])
Uniform random sampling of Euler roll-pitch-yaw angles, each in the range (-pi, pi]. The computed value has the order (roll, pitch, yaw). The return variable value is expected to already exist.
int halfNormalInt(int r_min, int r_max, double focus=3.0)
Generate a random integer using a half-normal distribution. The value is within specified bounds ([r_...
double uniform01()
Generate a random real between 0 and 1.
Definition: RandomNumbers.h:68
void uniformNormalVector(unsigned int n, double value[])
Uniform random sampling of a unit-length vector. I.e., the surface of an n-ball. The return variable ...
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:58
std::uint_fast32_t getLocalSeed() const
Get the seed used for the instance of a RNG. Passing the returned value to the setInstanceSeed() of a...
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: RandomNumbers.h:74
static std::uint_fast32_t getSeed()
Get the seed used to generate the seeds of each RNG instance. Passing the returned value to setSeed()...
double halfNormalReal(double r_min, double r_max, double focus=3.0)
Generate a random real using a half-normal distribution. The value is within specified bounds [r_min...
void setLocalSeed(std::uint_fast32_t localSeed)
Set the seed used for the instance of a RNG. Use this function to ensure that an instance of an RNG g...
static void setSeed(std::uint_fast32_t seed)
Set the seed used to generate the seeds of each RNG instance. Use this function to ensure the same se...
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound].
Definition: RandomNumbers.h:81
void uniformInBall(double r, unsigned int n, double value[])
Uniform random sampling of the content of an n-ball, with a radius appropriately distributed between ...
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and variance.