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 #include "ompl/util/ProlateHyperspheroid.h"
47 
48 namespace ompl
49 {
56  class RNG
57  {
58  public:
60  RNG();
61 
63  RNG(std::uint_fast32_t localSeed);
64 
66  double uniform01()
67  {
68  return uniDist_(generator_);
69  }
70 
72  double uniformReal(double lower_bound, double upper_bound)
73  {
74  assert(lower_bound <= upper_bound);
75  return (upper_bound - lower_bound) * uniDist_(generator_) + lower_bound;
76  }
77 
79  int uniformInt(int lower_bound, int upper_bound)
80  {
81  auto r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound) + 1.0));
82  return (r > upper_bound) ? upper_bound : r;
83  }
84 
86  bool uniformBool()
87  {
88  return uniDist_(generator_) <= 0.5;
89  }
90 
92  double gaussian01()
93  {
94  return normalDist_(generator_);
95  }
96 
98  double gaussian(double mean, double stddev)
99  {
100  return normalDist_(generator_) * stddev + mean;
101  }
102 
109  double halfNormalReal(double r_min, double r_max, double focus = 3.0);
110 
114  int halfNormalInt(int r_min, int r_max, double focus = 3.0);
115 
118  void quaternion(double value[4]);
119 
122  void eulerRPY(double value[3]);
123 
126  static void setSeed(std::uint_fast32_t seed);
127 
131  static std::uint_fast32_t getSeed();
132 
135  void setLocalSeed(std::uint_fast32_t localSeed);
136 
141  std::uint_fast32_t getLocalSeed() const
142  {
143  return localSeed_;
144  }
145 
148  void uniformNormalVector(std::vector<double> &v);
149 
153  void uniformInBall(double r, std::vector<double> &v);
154 
164  void uniformProlateHyperspheroidSurface(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr,
165  double value[]);
166 
176  void uniformProlateHyperspheroid(const std::shared_ptr<const ProlateHyperspheroid> &phsPtr, double value[]);
177 
178  private:
181  class SphericalData;
182 
184  std::uint_fast32_t localSeed_;
185  std::mt19937 generator_;
186  std::uniform_real_distribution<> uniDist_{0, 1};
187  std::normal_distribution<> normalDist_{0, 1};
188  // A structure holding boost::uniform_on_sphere distributions and the associated boost::variate_generators for
189  // various dimension
190  std::shared_ptr<SphericalData> sphericalDataPtr_;
191  };
192 } // namespace ompl
193 
194 #endif
void uniformNormalVector(std::vector< double > &v)
Uniform random sampling of a unit-length vector. I.e., the surface of an n-ball. The return variable ...
double gaussian01()
Generate a random real using a normal distribution with mean 0 and variance 1.
Definition: RandomNumbers.h:92
bool uniformBool()
Generate a random boolean.
Definition: RandomNumbers.h:86
void uniformProlateHyperspheroidSurface(const std::shared_ptr< const ProlateHyperspheroid > &phsPtr, double value[])
Uniform random sampling of the surface of a prolate hyperspheroid, a special symmetric type of n-dime...
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.
void uniformProlateHyperspheroid(const std::shared_ptr< const ProlateHyperspheroid > &phsPtr, double value[])
Uniform random sampling of a prolate hyperspheroid, a special symmetric type of n-dimensional ellipse...
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:66
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:56
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:72
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 uniformInBall(double r, std::vector< double > &v)
Uniform random sampling of the content of an n-ball, with a radius appropriately distributed between ...
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:79
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and variance.
Definition: RandomNumbers.h:98