00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef RNG_LATTICE_H
00012 #define RNG_LATTICE_H
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "Utility/SequenceGen.h"
00032
00033
00034 template<class T>
00035 class RNGLattice {
00036
00037 public:
00038
00039 typedef T Return_t;
00040
00041
00042 RNGLattice(T minval, T maxval, unsigned long numpoints,
00043 bool includeEndpoints = true)
00044 : MinVal(minval), MaxVal(maxval), CurrPoint(0), NumPoints(numpoints),
00045 IncludeEndpoints(includeEndpoints)
00046 {
00047 if (IncludeEndpoints) {
00048 if (NumPoints == 0 || NumPoints == 1) {
00049 Spacing = 0;
00050 }
00051 else {
00052 Spacing = (MaxVal - MinVal)/(NumPoints - 1);
00053 }
00054 }
00055 else {
00056 if (NumPoints == 0) {
00057 Spacing = 0;
00058 }
00059 else {
00060 Spacing = (MaxVal - MinVal)/NumPoints;
00061 }
00062 }
00063 }
00064
00065
00066 RNGLattice(const RNGLattice<T>& rng)
00067 : MinVal(rng.MinVal), MaxVal(rng.MaxVal), CurrPoint(rng.CurrPoint),
00068 Spacing(rng.Spacing), NumPoints(rng.NumPoints),
00069 IncludeEndpoints(rng.IncludeEndpoints) {}
00070
00071
00072 ~RNGLattice(void) {}
00073
00074
00075 inline void AdvanceSeed(unsigned long n = 0) {
00076 CurrPoint = (CurrPoint + n) % NumPoints;
00077 return;
00078 }
00079
00080
00081 inline void SetSeed(unsigned long n = 0) {
00082 CurrPoint = n % NumPoints;
00083 }
00084
00085
00086 inline unsigned long GetSeed(void) const { return CurrPoint; }
00087
00088
00089 inline Return_t GetRandom(void) const {
00090 Return_t currVal;
00091 if (IncludeEndpoints)
00092 currVal = MinVal + CurrPoint * Spacing;
00093 else
00094 currVal = MinVal + (CurrPoint + 0.5) * Spacing;
00095 CurrPoint++;
00096 if (CurrPoint == NumPoints) CurrPoint = 0;
00097 return currVal;
00098 }
00099
00100
00101 inline Return_t operator()(void) const { return GetRandom(); }
00102
00103
00104 inline operator Return_t() const { return GetRandom(); }
00105
00106
00107 unsigned long GetRandMax(void) const { return NumPoints; }
00108
00109 private:
00110 T MinVal, MaxVal, Spacing;
00111 mutable unsigned long CurrPoint;
00112 unsigned long NumPoints;
00113 bool IncludeEndpoints;
00114 };
00115
00116 #ifdef IPPL_USE_SINGLE_PRECISION
00117 RNG_BASIC_MATH(RNGLattice<float>)
00118 #else
00119 RNG_BASIC_MATH(RNGLattice<double>)
00120 #endif
00121
00122
00123
00124
00125
00126
00127 template<class T>
00128 class RNGLatticeSequence : public SequenceGen< RNGLattice<T> > {
00129
00130 public:
00131
00132 RNGLatticeSequence(T minval, T maxval, unsigned long numpoints,
00133 bool includeEndpoints = true)
00134 : SequenceGen< RNGLattice<T> >(RNGLattice<T>(minval,maxval,numpoints,
00135 includeEndpoints)) {}
00136
00137
00138 RNGLatticeSequence(const RNGLatticeSequence<T>& rngseq)
00139 : SequenceGen< RNGLattice<T> >(rngseq.getGenerator()) {}
00140
00141
00142 ~RNGLatticeSequence(void) {}
00143
00144
00145 inline void AdvanceSeed(unsigned long adv = 0) {
00146 this->getGenerator().AdvanceSeed(adv);
00147 }
00148 inline void SetSeed(unsigned long seed) { this->getGenerator().SetSeed(seed); }
00149 inline unsigned long GetSeed(void) const { return this->getGenerator().GetSeed(); }
00150 inline
00151 typename RNGLattice<T>::Return_t
00152 GetRandom(void) { return this->getGenerator().GetRandom(); }
00153 inline
00154 typename RNGLattice<T>::Return_t
00155 operator()(void) { return this->getGenerator().GetRandom(); }
00156 unsigned long GetRandMax(void) const { return this->getGenerator().GetRandMax(); }
00157 };
00158
00159
00160 #endif // RNG_LATTICE_H
00161
00162
00163
00164
00165
00166
00167