00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SALT_RANDOM_H
00023 #define SALT_RANDOM_H
00024
00025 #include <boost/random.hpp>
00026 #include <boost/version.hpp>
00027
00028 namespace salt
00029 {
00030 #if 0
00031 }
00032 #endif
00033
00045 class RandomEngine : public boost::mt19937
00046 {
00047 public:
00048 typedef boost::mt19937::result_type result_type;
00049
00050 static RandomEngine& instance()
00051 { static std::auto_ptr<RandomEngine> the_instance(new RandomEngine());
00052 return *the_instance; }
00053
00054 static RandomEngine&
00055 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
00056
00057 instance(const result_type& value)
00058 #else
00059 instance(result_type value)
00060 #endif
00061 { instance().seed( value ); return instance(); }
00062
00063
00064
00065
00066 template<class Generator>
00067 static RandomEngine& instance(Generator& gen)
00068 { instance().seed( gen ); return instance(); }
00069 private:
00070 RandomEngine() : boost::mt19937() { }
00071 };
00072
00073 #if (defined(BOOST_VERSION) && (BOOST_VERSION >= 103100))
00074 #include <boost/random/variate_generator.hpp>
00075
00079 template<class RealType = double>
00080 class UniformRNG : public boost::variate_generator<salt::RandomEngine,
00081 boost::uniform_real<RealType> >
00082 {
00083 public:
00084 UniformRNG(RealType min = RealType(0), RealType max = RealType(1))
00085 : boost::variate_generator<RandomEngine, boost::uniform_real<RealType> >
00086 (salt::RandomEngine::instance(), boost::uniform_real<RealType>(min,max))
00087 {}
00088 };
00089
00092 template<class RealType = double>
00093 class NormalRNG : public boost::variate_generator<salt::RandomEngine,
00094 boost::normal_distribution<RealType> >
00095 {
00096 public:
00097 NormalRNG(double mean, double sigma = (1))
00098 : boost::variate_generator<RandomEngine,
00099 boost::normal_distribution<RealType> >
00100 (salt::RandomEngine::instance(), boost::normal_distribution<RealType>(mean,sigma))
00101 {}
00102 };
00103
00108 template<class RealType = double>
00109 class ExponentialRNG : public boost::variate_generator<salt::RandomEngine,
00110 boost::exponential_distribution<RealType> >
00111 {
00112 public:
00113 ExponentialRNG(double lambda = RealType(1))
00114 : boost::variate_generator<RandomEngine,
00115 boost::exponential_distribution<RealType> >
00116 (salt::RandomEngine::instance(),
00117 boost::exponential_distribution<RealType>(lambda))
00118 {}
00119 };
00120
00121 #else // BOOST_VERSION >= 103100
00122
00126 template<class RealType = double>
00127 class UniformRNG : public boost::uniform_real<salt::RandomEngine,RealType>
00128 {
00129 public:
00130 UniformRNG(RealType min = RealType(0), RealType max = RealType(1))
00131 : boost::uniform_real<RandomEngine,RealType>
00132 (salt::RandomEngine::instance(),min,max)
00133 {}
00134 };
00135
00138 template<class RealType = double>
00139 class NormalRNG : public boost::normal_distribution<salt::RandomEngine>
00140 {
00141 public:
00142 NormalRNG(double mean, double sigma = (1))
00143 : boost::normal_distribution<salt::RandomEngine>
00144 (salt::RandomEngine::instance(),mean,sigma)
00145 {}
00146 };
00147
00152 template<class RealType = double>
00153 class ExponentialRNG : public boost::exponential_distribution<salt::RandomEngine>
00154 {
00155 public:
00156 ExponentialRNG(double lambda = RealType(1))
00157 : boost::exponential_distribution<salt::RandomEngine>
00158 (salt::RandomEngine::instance(),lambda)
00159 {}
00160 };
00161
00162 #endif
00163
00164 }
00165
00166 #endif // SALT_RANDOM_H