random/default.h

Go to the documentation of this file.
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     typedef typename IRNG::T_state T_state;
00048     T_state getState() const { return irng_.getState(); }
00049     std::string getStateString() const { return irng_.getStateString(); }
00050     void setState(const T_state& s) { irng_.setState(s); }
00051     void setState(const std::string& s) { irng_.setState(s); }
00052 
00053 protected:
00054     static IRNG irng_;
00055 };
00056 
00057 template<typename IRNG>
00058 IRNG IRNGWrapper<IRNG,sharedState>::irng_;
00059 
00060 template<typename IRNG>
00061 class IRNGWrapper<IRNG,independentState> {
00062 
00063 public:
00064     void seed(IRNG_int x)
00065     { irng_.seed(x); }
00066 
00067     typedef typename IRNG::T_state T_state;
00068     T_state getState() const { return irng_.getState(); }
00069     std::string getStateString() const { return irng_.getStateString(); }
00070     void setState(const T_state& s) { irng_.setState(s); }
00071     void setState(const std::string& s) { irng_.setState(s); }
00072 
00073 protected:
00074     IRNG irng_;
00075 };
00076 
00077 // defaultIRNG is a type alias for the default Integer Random
00078 // Number Generator (IRNG).
00079 
00080 typedef MersenneTwister defaultIRNG;
00081 
00082 BZ_NAMESPACE_END
00083 
00084 #endif // BZ_RANDOM_DEFAULT_H
00085 

Generated on Mon Dec 3 09:21:55 2007 for blitz by  doxygen 1.5.1