random_numbers.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
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)(
00053       (boost::posix_time::microsec_clock::universal_time() - boost::posix_time::ptime(boost::date_time::min_date_time))
00054           .total_microseconds() +
00055       (unsigned long long)(mem.get()));
00056   return first_seed_;
00057 }
00058 
00062 static boost::uint32_t nextSeed(void)
00063 {
00064   static boost::mutex rngMutex;
00065   boost::mutex::scoped_lock slock(rngMutex);
00066   static boost::lagged_fibonacci607 sGen(firstSeed());
00067   static boost::uniform_int<> sDist(1, 1000000000);
00068   static boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > s(sGen, sDist);
00069   boost::uint32_t v = s();
00070   return v;
00071 }
00072 
00073 random_numbers::RandomNumberGenerator::RandomNumberGenerator(void)
00074   : generator_(nextSeed())
00075   , uniDist_(0, 1)
00076   , normalDist_(0, 1)
00077   , uni_(generator_, uniDist_)
00078   , normal_(generator_, normalDist_)
00079 {
00080 }
00081 
00082 random_numbers::RandomNumberGenerator::RandomNumberGenerator(boost::uint32_t seed)
00083   : generator_(seed), uniDist_(0, 1), normalDist_(0, 1), uni_(generator_, uniDist_), normal_(generator_, normalDist_)
00084 {
00085   // Because we manually specified a seed, we need to save it ourselves
00086   first_seed_ = seed;
00087 }
00088 
00089 // From: "Uniform Random Rotations", Ken Shoemake, Graphics Gems III,
00090 //       pg. 124-132
00091 void random_numbers::RandomNumberGenerator::quaternion(double value[4])
00092 {
00093   double x0 = uni_();
00094   double r1 = sqrt(1.0 - x0), r2 = sqrt(x0);
00095   double t1 = 2.0 * boost::math::constants::pi<double>() * uni_(),
00096          t2 = 2.0 * boost::math::constants::pi<double>() * uni_();
00097   double c1 = cos(t1), s1 = sin(t1);
00098   double c2 = cos(t2), s2 = sin(t2);
00099   value[0] = s1 * r1;
00100   value[1] = c1 * r1;
00101   value[2] = s2 * r2;
00102   value[3] = c2 * r2;
00103 }
00104 
00105 boost::uint32_t random_numbers::RandomNumberGenerator::getFirstSeed()
00106 {
00107   return first_seed_;
00108 }


random_numbers
Author(s): Ioan Sucan
autogenerated on Wed Apr 3 2019 02:54:25