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 */
36 
37 #ifndef OMPL_UTIL_RANDOM_NUMBERS_
38 #define OMPL_UTIL_RANDOM_NUMBERS_
39 
40 #include <boost/random/mersenne_twister.hpp>
41 #include <boost/random/uniform_real.hpp>
42 #include <boost/random/normal_distribution.hpp>
43 #include <boost/random/variate_generator.hpp>
44 #include <cassert>
45 
46 namespace ompl
47 {
54  class RNG
55  {
56  public:
57 
59  RNG();
60 
62  double uniform01()
63  {
64  return uni_();
65  }
66 
68  double uniformReal(double lower_bound, double upper_bound)
69  {
70  assert(lower_bound <= upper_bound);
71  return (upper_bound - lower_bound) * uni_() + lower_bound;
72  }
73 
75  int uniformInt(int lower_bound, int upper_bound)
76  {
77  int r = (int)floor(uniformReal((double)lower_bound, (double)(upper_bound) + 1.0));
78  return (r > upper_bound) ? upper_bound : r;
79  }
80 
82  bool uniformBool()
83  {
84  return uni_() <= 0.5;
85  }
86 
88  double gaussian01()
89  {
90  return normal_();
91  }
92 
94  double gaussian(double mean, double stddev)
95  {
96  return normal_() * stddev + mean;
97  }
98 
104  double halfNormalReal(double r_min, double r_max, double focus = 3.0);
105 
109  int halfNormalInt(int r_min, int r_max, double focus = 3.0);
110 
112  void quaternion(double value[4]);
113 
115  void eulerRPY(double value[3]);
116 
120  static void setSeed(boost::uint32_t seed);
121 
126  static boost::uint32_t getSeed();
127 
128  private:
129 
130  boost::mt19937 generator_;
131  boost::uniform_real<> uniDist_;
132  boost::normal_distribution<> normalDist_;
133  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > uni_;
134  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > normal_;
135 
136  };
137 
138 }
139 
140 #endif
double gaussian01()
Generate a random real using a normal distribution with mean 0 and variance 1.
Definition: RandomNumbers.h:88
static void setSeed(boost::uint32_t seed)
Set the seed for random number generation. Use this function to ensure the same sequence of random nu...
bool uniformBool()
Generate a random boolean.
Definition: RandomNumbers.h:82
void quaternion(double value[4])
Uniform random unit quaternion sampling. The computed value has the order (x,y,z,w) ...
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)
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:62
Main namespace. Contains everything in this library.
Definition: Cost.h:42
static boost::uint32_t getSeed()
Get the seed used for random number generation. Passing the returned value to setSeed() at a subseque...
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:54
double uniformReal(double lower_bound, double upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: RandomNumbers.h:68
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...
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound].
Definition: RandomNumbers.h:75
double gaussian(double mean, double stddev)
Generate a random real using a normal distribution with given mean and variance.
Definition: RandomNumbers.h:94