00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef RNG_RAND_H
00012 #define RNG_RAND_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 #include <stdlib.h>
00033
00034 class RNGRand {
00035
00036 public:
00037
00038 typedef double Return_t;
00039
00040 public:
00041
00042 RNGRand(int advance = 0) : CurrentSeed(1U) { AdvanceSeed(advance); }
00043
00044
00045 RNGRand(const RNGRand& rng) : CurrentSeed(rng.CurrentSeed) {}
00046
00047
00048 ~RNGRand(void) {}
00049
00050
00051
00052
00053 inline void AdvanceSeed(int advance = 0) {
00054 for (int iadv=0; iadv<advance; iadv++) {
00055
00056 srand(CurrentSeed);
00057
00058 int seedstep = rand();
00059 CurrentSeed = (CurrentSeed + seedstep) % RAND_MAX;
00060 }
00061
00062 srand(CurrentSeed);
00063 }
00064
00065
00066 inline void SetSeed(unsigned int seed) {
00067 CurrentSeed = seed;
00068 srand(CurrentSeed);
00069 }
00070
00071
00072 inline unsigned int GetSeed(void) const { return CurrentSeed; }
00073
00074
00075 inline Return_t GetRandom(void) const {
00076 return ( (Return_t(rand())) / (Return_t(RAND_MAX)+1) );
00077 }
00078
00079
00080 inline Return_t operator()(void) const { return GetRandom(); }
00081
00082
00083 inline operator Return_t() const { return GetRandom(); }
00084
00085
00086 static Return_t GetRandMax(void) { return Return_t(RAND_MAX)+1; }
00087
00088 private:
00089 unsigned int CurrentSeed;
00090 };
00091
00092 RNG_BASIC_MATH(RNGRand)
00093
00094
00095
00096
00097
00098
00099
00100 class RNGRandSequence : public SequenceGen<RNGRand> {
00101
00102 public:
00103
00104 RNGRandSequence(int advance = 0)
00105 : SequenceGen<RNGRand>(RNGRand(advance)) {}
00106
00107
00108 RNGRandSequence(const RNGRandSequence& rngseq)
00109 : SequenceGen<RNGRand>(rngseq.getGenerator()) {}
00110
00111
00112 ~RNGRandSequence(void) {}
00113
00114
00115 inline void AdvanceSeed(int adv = 0) { getGenerator().AdvanceSeed(adv); }
00116 inline void SetSeed(unsigned int seed) { getGenerator().SetSeed(seed); }
00117 inline unsigned int GetSeed(void) const { return getGenerator().GetSeed(); }
00118 inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
00119 inline Return_t operator()(void) { return getGenerator().GetRandom(); }
00120 static Return_t GetRandMax(void) { return RNGRand::GetRandMax(); }
00121 };
00122
00123
00124 #endif // RNG_RAND_H
00125
00126
00127
00128
00129
00130
00131