Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "random_numbers/random_numbers.h"
00038
00039 #include <boost/random/lagged_fibonacci.hpp>
00040 #include <boost/random/uniform_int.hpp>
00041 #include <boost/thread/mutex.hpp>
00042 #include <boost/date_time/posix_time/posix_time.hpp>
00043 #include <boost/math/constants/constants.hpp>
00044 #include <boost/scoped_ptr.hpp>
00045
00046 static boost::uint32_t first_seed_ = 0;
00047
00049 static boost::uint32_t firstSeed(void)
00050 {
00051 boost::scoped_ptr<int> mem(new int());
00052 first_seed_ = (boost::uint32_t)((boost::posix_time::microsec_clock::universal_time() -
00053 boost::posix_time::ptime(boost::date_time::min_date_time)).total_microseconds() +
00054 (unsigned long long)(mem.get()));
00055 return first_seed_;
00056 }
00057
00061 static boost::uint32_t nextSeed(void)
00062 {
00063 static boost::mutex rngMutex;
00064 boost::mutex::scoped_lock slock(rngMutex);
00065 static boost::lagged_fibonacci607 sGen(firstSeed());
00066 static boost::uniform_int<> sDist(1, 1000000000);
00067 static boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > s(sGen, sDist);
00068 boost::uint32_t v = s();
00069 return v;
00070 }
00071
00072
00073 random_numbers::RandomNumberGenerator::RandomNumberGenerator(void) : generator_(nextSeed()),
00074 uniDist_(0, 1),
00075 normalDist_(0, 1),
00076 uni_(generator_, uniDist_),
00077 normal_(generator_, normalDist_)
00078 {
00079 }
00080
00081 random_numbers::RandomNumberGenerator::RandomNumberGenerator(boost::uint32_t seed)
00082 : generator_(seed),
00083 uniDist_(0, 1),
00084 normalDist_(0, 1),
00085 uni_(generator_, uniDist_),
00086 normal_(generator_, normalDist_)
00087 {
00088
00089 first_seed_ = seed;
00090 }
00091
00092
00093
00094 void random_numbers::RandomNumberGenerator::quaternion(double value[4])
00095 {
00096 double x0 = uni_();
00097 double r1 = sqrt(1.0 - x0), r2 = sqrt(x0);
00098 double t1 = 2.0 * boost::math::constants::pi<double>() * uni_(), t2 = 2.0 * boost::math::constants::pi<double>() * uni_();
00099 double c1 = cos(t1), s1 = sin(t1);
00100 double c2 = cos(t2), s2 = sin(t2);
00101 value[0] = s1 * r1;
00102 value[1] = c1 * r1;
00103 value[2] = s2 * r2;
00104 value[3] = c2 * r2;
00105 }
00106
00107 boost::uint32_t random_numbers::RandomNumberGenerator::getFirstSeed()
00108 {
00109 return first_seed_;
00110 }