📄 rand_num_generator.hpp
字号:
#ifndef RAND_NUM_GENERATOR_H #define RAND_NUM_GENERATOR_H #include <iostream> using namespace std; #include <boost/shared_ptr.hpp> #include <boost/random.hpp> #include <boost/utility.hpp> #include "utility.hpp" class RandNumGenerator : boost::noncopyable { public: typedef boost::shared_ptr<RandNumGenerator> RandNumGeneratorPtr; static inline RandNumGeneratorPtr create(); inline void setSeed(const t_uint seed); inline int uniformInt(const int min, const int max); inline double uniformZeroOne(); inline double uniformReal(const double min, const double max); inline double exponential(const double lambda); inline double normalDistribution(const double mean, const double sigma); friend ostream& operator<< (ostream& s, const RandNumGenerator& randNumGenerator); private: typedef boost::lagged_fibonacci607 BaseGenerator; static const t_uint m_DEFAULT_SEED; t_uint m_seed; BaseGenerator m_baseGenerator; RandNumGenerator(); }; typedef boost::shared_ptr<RandNumGenerator> RandNumGeneratorPtr; // Inline Functions inline RandNumGeneratorPtr RandNumGenerator::create() { RandNumGeneratorPtr rand(new RandNumGenerator()); return rand; } inline void RandNumGenerator::setSeed(t_uint seed) { m_seed = seed; m_baseGenerator.seed(m_seed); } inline int RandNumGenerator::uniformInt(const int min, const int max) { boost::uniform_int<> uniformInt(min, max); boost::variate_generator<BaseGenerator&, boost::uniform_int<> > generator(m_baseGenerator, uniformInt); int randVal = generator(); return randVal; } inline double RandNumGenerator::uniformZeroOne() { boost::uniform_01<BaseGenerator> generator(m_baseGenerator); double randVal = generator(); return randVal; } inline double RandNumGenerator::uniformReal(const double min, const double max) { boost::uniform_real<> uniformReal(min, max); boost::variate_generator<BaseGenerator&, boost::uniform_real<> > generator(m_baseGenerator, uniformReal); double randVal = generator(); return randVal; } inline double RandNumGenerator::exponential(const double lambda) { boost::exponential_distribution<> exponentialDist(lambda); boost::variate_generator<BaseGenerator&, boost::exponential_distribution<> > generator(m_baseGenerator, exponentialDist); double randVal = generator(); return randVal; } inline double RandNumGenerator::normalDistribution(const double mean, const double sigma) { boost::normal_distribution<> normalDist(mean, sigma); boost::variate_generator<BaseGenerator&, boost::normal_distribution<> > generator(m_baseGenerator, normalDist); double randVal = generator(); return randVal; } // Overloaded Operators inline ostream& operator<< (ostream& s, const RandNumGenerator& randNumGenerator) { return s<< "RandNumGenerator state (pointer=" << &randNumGenerator << ", seed=" << randNumGenerator.m_seed << ")" ; } #endif // RAND_NUM_GENERATOR_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -