37 #include "ompl/util/RandomNumbers.h"
38 #include "ompl/util/Exception.h"
40 #include <boost/random/lagged_fibonacci.hpp>
41 #include <boost/random/uniform_int.hpp>
42 #include <boost/thread/mutex.hpp>
43 #include <boost/date_time/posix_time/posix_time.hpp>
44 #include <boost/math/constants/constants.hpp>
47 static boost::uint32_t& getUserSetSeed(
void)
49 static boost::uint32_t userSetSeed = 0;
54 static bool& getFirstSeedGenerated(
void)
56 static bool firstSeedGenerated =
false;
57 return firstSeedGenerated;
61 static boost::uint32_t firstSeed(
void)
64 static boost::uint32_t firstSeedValue = 0;
66 static boost::mutex fsLock;
67 boost::mutex::scoped_lock slock(fsLock);
69 if (getFirstSeedGenerated())
70 return firstSeedValue;
72 if (getUserSetSeed() != 0)
73 firstSeedValue = getUserSetSeed();
76 (boost::uint32_t)(boost::posix_time::microsec_clock::universal_time() -
77 boost::posix_time::ptime(boost::date_time::min_date_time)).total_microseconds();
78 getFirstSeedGenerated() =
true;
80 return firstSeedValue;
86 static boost::uint32_t nextSeed(
void)
88 static boost::mutex rngMutex;
89 boost::mutex::scoped_lock slock(rngMutex);
90 static boost::lagged_fibonacci607 sGen(firstSeed());
91 static boost::uniform_int<> sDist(1, 1000000000);
92 static boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > s(sGen, sDist);
103 if (getFirstSeedGenerated())
105 OMPL_ERROR(
"Random number generation already started. Changing seed now will not lead to deterministic sampling.");
109 OMPL_WARN(
"Random generator seed cannot be 0. Using 1 instead.");
110 getUserSetSeed() = 1;
113 getUserSetSeed() = seed;
119 uni_(generator_, uniDist_),
120 normal_(generator_, normalDist_)
126 assert(r_min <= r_max);
128 const double mean = r_max - r_min;
129 double v = gaussian(mean, mean/focus);
131 if (v > mean) v = 2.0 * mean - v;
132 double r = v >= 0.0 ? v + r_min : r_min;
133 return r > r_max ? r_max : r;
138 int r = (int)floor(halfNormalReal((
double)r_min, (
double)(r_max) + 1.0, focus));
139 return (r > r_max) ? r_max : r;
147 double r1 = sqrt(1.0 - x0), r2 = sqrt(x0);
148 double t1 = 2.0 * boost::math::constants::pi<double>() * uni_(), t2 = 2.0 * boost::math::constants::pi<double>() * uni_();
149 double c1 = cos(t1), s1 = sin(t1);
150 double c2 = cos(t2), s2 = sin(t2);
160 value[0] = boost::math::constants::pi<double>() * (-2.0 * uni_() + 1.0);
161 value[1] = acos(1.0 - 2.0 * uni_()) - boost::math::constants::pi<double>() / 2.0;
162 value[2] = boost::math::constants::pi<double>() * (-2.0 * uni_() + 1.0);
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...
void quaternion(double value[4])
Uniform random unit quaternion sampling. The computed value has the order (x,y,z,w) ...
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_...
RNG(void)
Constructor. Always sets a different random seed.
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
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...
static boost::uint32_t getSeed(void)
Get the seed used for random number generation. Passing the returned value to setSeed() at a subseque...