00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #ifndef RNG_SIMPLE_H
00012 #define RNG_SIMPLE_H
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include "Utility/SequenceGen.h"
00031 
00032 
00033 class RNGSimple {
00034 
00035 public:
00036   
00037   typedef double Return_t;
00038 
00039 public:
00040   
00041   RNGSimple(int advance = 0)
00042     : CurrentSeed(RandShift + 1), CurrentRand(0) { AdvanceSeed(advance); }
00043 
00044   
00045   RNGSimple(const RNGSimple& rng)
00046     : CurrentSeed(rng.CurrentSeed), CurrentRand(rng.CurrentRand) { }
00047 
00048   
00049   ~RNGSimple(void) {}
00050 
00051   
00052   inline void AdvanceSeed(int advance = 0) {
00053     for (int iadv=0; iadv<advance; iadv++) 
00054       CurrentSeed = (CurrentSeed + RandShift) % RandModulus;
00055     CurrentRand = CurrentSeed;
00056   }
00057 
00058   
00059   inline void SetSeed(unsigned long seed) {
00060     CurrentSeed = (seed + RandShift) % RandModulus;
00061     CurrentRand = CurrentSeed;
00062   }
00063 
00064   
00065   inline unsigned long GetSeed(void) const { return CurrentSeed; }
00066 
00067   
00068   inline Return_t GetRandom(void) const {
00069     CurrentRand = (CurrentRand * RandMultipplier + RandShift) % RandModulus;
00070     return ( Return_t(CurrentRand) / Return_t(RandModulus) );
00071   }
00072 
00073   
00074   inline Return_t operator()(void) const { return GetRandom(); }
00075 
00076   
00077   inline operator Return_t() const { return GetRandom(); }
00078 
00079   
00080   static Return_t GetRandMax(void) { return Return_t(RandModulus); }
00081 
00082 private:
00083   long CurrentSeed;
00084   mutable long CurrentRand;
00085 
00086   static const long RandModulus;
00087   static const long RandMultipplier;
00088   static const long RandShift;
00089 };
00090 
00091 RNG_BASIC_MATH(RNGSimple)
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 class RNGSimpleSequence : public SequenceGen<RNGSimple> {
00100 
00101 public:
00102   
00103   RNGSimpleSequence(int advance = 0)
00104     : SequenceGen<RNGSimple>(RNGSimple(advance)) {}
00105 
00106   
00107   RNGSimpleSequence(const RNGSimpleSequence& rngseq)
00108     : SequenceGen<RNGSimple>(rngseq.getGenerator()) {}
00109 
00110   
00111   ~RNGSimpleSequence(void) {}
00112 
00113   
00114   inline void     AdvanceSeed(int adv = 0) { getGenerator().AdvanceSeed(adv); }
00115   inline void     SetSeed(unsigned long seed) { getGenerator().SetSeed(seed); }
00116   inline unsigned long GetSeed(void) const { return getGenerator().GetSeed(); }
00117   inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
00118   inline Return_t operator()(void) { return getGenerator().GetRandom(); }
00119   static Return_t GetRandMax(void) { return RNGSimple::GetRandMax(); }
00120 };
00121 
00122 
00123 #endif // RNG_SIMPLE_H
00124 
00125 
00126 
00127 
00128 
00129 
00130