00001 #ifndef BZ_RANDOM_DEFAULT_H 00002 #define BZ_RANDOM_DEFAULT_H 00003 00004 #include <random/mt.h> 00005 00006 BZ_NAMESPACE(ranlib) 00007 00008 // Some terminology: 00009 // IRNG = Integer Random Number Generator. IRNGs generate random 00010 // integers, which are used to create floating-point random 00011 // numbers. 00012 // RNG = Random Number Generator. RNGs use IRNGs to create floating- 00013 // point random numbers following desired distributions. 00014 00015 typedef float defaultType; 00016 00017 // These are type tags. A RNG with sharedState shares an IRNG 00018 // with other RNGs. An RNG with independentState 00019 // contains its own IRNG. Generally, sharedState RNGs should be 00020 // used. 00021 00022 struct sharedState { }; 00023 struct independentState { }; 00024 typedef sharedState defaultState; 00025 00026 typedef unsigned int IRNG_int; 00027 00028 00029 // IRNGWrapper handles shared and independent state IRNGs. 00030 // If a class inherits from IRNGWrapper<IRNG,sharedState>, 00031 // it gets a static IRNG (i.e. the IRNG state is shared among 00032 // all RNGs); if it inherits from IRNGWrapper<IRNG,independentState>, 00033 // it gets an independent IRNG (the IRNG state is encapsulated 00034 // in the RNG, and is not shared among RNGs). 00035 00036 template<typename IRNG, typename state> 00037 class IRNGWrapper { 00038 }; 00039 00040 template<typename IRNG> 00041 class IRNGWrapper<IRNG,sharedState> { 00042 00043 public: 00044 void seed(IRNG_int x) 00045 { irng_.seed(x); } 00046 00047 void seed(std::vector<IRNG_int> x) 00048 { irng_.seed(x); } 00049 00050 typedef typename IRNG::T_state T_state; 00051 T_state getState() const { return irng_.getState(); } 00052 std::string getStateString() const { return irng_.getStateString(); } 00053 void setState(const T_state& s) { irng_.setState(s); } 00054 void setState(const std::string& s) { irng_.setState(s); } 00055 00056 protected: 00057 static IRNG irng_; 00058 }; 00059 00060 template<typename IRNG> 00061 IRNG IRNGWrapper<IRNG,sharedState>::irng_; 00062 00063 template<typename IRNG> 00064 class IRNGWrapper<IRNG,independentState> { 00065 00066 public: 00067 IRNGWrapper() {}; 00068 IRNGWrapper(unsigned int i) : irng_(MersenneTwisterCreator::create(i)) {}; 00069 00070 void seed(IRNG_int x) 00071 { irng_.seed(x); } 00072 00073 void seed(std::vector<IRNG_int> x) 00074 { irng_.seed(x); } 00075 00076 typedef typename IRNG::T_state T_state; 00077 T_state getState() const { return irng_.getState(); } 00078 std::string getStateString() const { return irng_.getStateString(); } 00079 void setState(const T_state& s) { irng_.setState(s); } 00080 void setState(const std::string& s) { irng_.setState(s); } 00081 00082 protected: 00083 IRNG irng_; 00084 }; 00085 00086 // defaultIRNG is a type alias for the default Integer Random 00087 // Number Generator (IRNG). 00088 00089 typedef MersenneTwister defaultIRNG; 00090 00091 BZ_NAMESPACE_END 00092 00093 #endif // BZ_RANDOM_DEFAULT_H 00094