Program Listing for File random_numbers.h

Return to documentation for file (include/random_numbers/random_numbers.h)

// Copyright 2008 Willow Garage, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the following disclaimer.
//
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the following disclaimer in the
//      documentation and/or other materials provided with the distribution.
//
//    * Neither the name of the Willow Garage, Inc. nor the names of its
//      contributors may be used to endorse or promote products derived from
//      this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

/* Author: Ioan Sucan */

#ifndef RANDOM_NUMBERS_RANDOM_NUMBERS_
#define RANDOM_NUMBERS_RANDOM_NUMBERS_

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <boost/random/normal_distribution.hpp>

namespace random_numbers
{
class RandomNumberGenerator
{
public:
  RandomNumberGenerator(void);

  RandomNumberGenerator(boost::uint32_t seed);

  double uniform01(void)
  {
    return uni_();
  }

  double uniformReal(double lower_bound, double upper_bound)
  {
    return (upper_bound - lower_bound) * uni_() + lower_bound;
  }

  double gaussian01(void)
  {
    return normal_();
  }

  double gaussian(double mean, double stddev)
  {
    return normal_() * stddev + mean;
  }

  void quaternion(double value[4]);

  int uniformInteger(int min, int max)
  {
    boost::uniform_int<> dis(min, max);
    return dis(generator_);
  }

  boost::uint32_t getFirstSeed();

private:
  boost::mt19937 generator_;
  boost::uniform_real<> uniDist_;
  boost::normal_distribution<> normalDist_;
  boost::variate_generator<boost::mt19937&, boost::uniform_real<> > uni_;
  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > normal_;
};
}  // namespace random_numbers

#endif