// %pacpus:license{ // This file is part of the PACPUS framework distributed under the // CECILL-C License, Version 1.0. // %pacpus:license} /// @file /// @author Firstname Surname /// @date Month, Year /// @version $Id: rng.hpp 76 2013-01-10 17:05:10Z kurdejma $ /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved. /// @brief Random number generators. /// /// Detailed description. #ifndef __BOOST_RNG__ #define __BOOST_RNG__ #include #include #include namespace math { namespace rng { /// This class is basic normal random number generator /// /// @tparam RealType describe number precision, e.g. @c float, @c double /// @tparam PRNG describe pseudo random number generator /// @see boost::random template class normal_generator { public: /// Default construtor normal_generator() : engine(time(NULL)) , generator(engine,boost::normal_distribution(0,1)) {} /// Construtor /// /// @param dist a normal distribution /// @see boost::distributions normal_generator(const boost::math::normal_distribution & dist) : engine(time(NULL)) , generator(engine,boost::normal_distribution(dist.mean(),dist.sigma())) {} /// Constructor /// /// @param mean mean of the normal distribution /// @param sigma standard deviation of the normal distribution normal_generator(const RealType & mean ,const RealType & sigma) : engine(time(NULL)) , generator(engine,boost::normal_distribution(mean(),sigma())) {} /// Get a random number RealType operator()() { return generator(); } /// Set normal distribution /// /// @param dist : a normal distribution /// @see boost::distributions void distribution( const boost::math::normal_distribution & dist) { generator.dist= boost::normal_distribution(dist.mean(),dist.standard_deviation()); } /// Set normal distrubution /// /// @param mean : mean of the normal distribution /// @param sigma : satndard deviation of the normal distribution void distribution( const RealType & mean ,const RealType & sigma) { generator.distribution()= boost::normal_distribution(mean,sigma); } private: /// pseudorandom number generator PRNG engine; /// the normal random number generator boost::variate_generator > generator; }; /// Defines normal random number generator as a Mersenne Twister 19937 with double-precision numbers typedef class normal_generator normal_rng; /// This class is basic uniform random number generator. /// /// @tparam RealType describe number precision, e.g. @c float, @c double /// @tparam PRNG describe pseudo random number generator /// @see boost::random template class uniform_generator { public: /// Default construtor uniform_generator() : engine(time(NULL)) , generator(engine, boost::uniform_real(0,1)) {} /// Construtor /// @param dist a uniform distribution /// @see boost::distributions uniform_generator(const boost::math::uniform_distribution & dist) : engine(time(NULL)) , generator(engine, boost::uniform_real(dist.lower(),dist.upper())) {} /// Constructor /// /// @param lower the lower value of the uniform distribution /// @param upper the upper value of the uniform distribution uniform_generator(const RealType & lower , const RealType & upper) : engine(time(NULL)) , generator(engine, boost::uniform_real(lower,upper)) {} /// Get a random number RealType operator()() { return generator(); } /// Set uniform distribution /// /// @param dist a normal distribution /// @see boost::distributions void distribution( const boost::math::uniform_distribution & dist) { generator.dist()= boost::uniform_real(dist.lower(),dist.upper()); } /// Set uniform distribution /// /// @param lower the lower value of the uniform distribution /// @param upper the upper value of the uniform distribution void distribution( const RealType & lower , const RealType & upper) { generator.dist()= boost::uniform_real(lower,upper); } private: /// @brief pseudo random number generator PRNG engine; /// @brief the uniform random number generator boost::variate_generator > generator; }; /// Defines uniform random number generator as a Mersenne Twister 19937 with double-precision numbers typedef class uniform_generator uniform_rng; /// This class is basic triangular random number generator /// /// @tparam RealType describe number precision, e.g. @c float, @c double /// @tparam PRNG describe pseudo random number generator ( see boost random) template class triangle_generator { public: /// Default construtor triangle_generator() : engine(time(NULL)) , generator(engine, boost::triangle_distribution(-1,0,1)) {} /// Construtor /// /// @param dist : a traingle distribution /// @see boost distributions triangle_generator(const boost::math::triangular_distribution & dist) : engine(time(NULL)) ,generator(engine, boost::triangle_distribution(dist.lower(),dist.upper())) {} /// Constructor /// /// @param lower the lower value of the uniform distribution /// @param mode the mode value of the uniform distribution /// @param upper the upper value of the uniform distribution triangle_generator(const RealType & lower, const RealType & mode ,const RealType & upper) : engine(time(NULL)) , generator(engine, boost::triangle_distribution(lower,mode,upper)) {} /// Get a random number RealType operator()() { return generator(); } /// Set triangle distribution /// @param dist a normal distribution /// @see boost::distributions void distribution( const boost::math::triangular_distribution & dist) { generator.dist()= boost::triangle_distribution(dist.lower(),dist.mode(),dist.upper()); } /// Set triangle distribution /// @param lower the lower value of the uniform distribution /// @param mode the mode value of the uniform distribution /// @param upper the upper value of the uniform distribution void distribution( const RealType & lower, const RealType & mode ,const RealType & upper) { generator.dist()= boost::triangle_distribution(lower,mode,upper); } private : /// pseudorandom number generator PRNG engine; /// the triangle random number generator boost::variate_generator > generator; }; typedef class triangle_generator triangle_rng; } // namespace rng } // namespace math #endif // __BOOST_RNG__