src/Utility/RNGSimple.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef RNG_SIMPLE_H
00012 #define RNG_SIMPLE_H
00013 
00014 /***********************************************************************
00015  * 
00016  * class RNGSimple
00017  * class RNGSimpleSequence : public SequenceGen<RNGSimple>
00018  *
00019  * Simple class that implements random number generator a la
00020  * Numerical Recipes, in the range [0...1].  The first class may be
00021  * used standalone, or as a template parameter to SequenceGen.  The
00022  * second class is derived from SequenceGen, and makes it easier to
00023  * use this RNG in expressions.
00024  * Use RNGSimple as a scalar or container element, and use
00025  * RNGSimpleSequence when you need a sequence of numbers to fill a container.
00026  *
00027  ***********************************************************************/
00028 
00029 // include files
00030 #include "Utility/SequenceGen.h"
00031 
00032 
00033 class RNGSimple {
00034 
00035 public:
00036   // return type
00037   typedef double Return_t;
00038 
00039 public:
00040   // default constructor
00041   RNGSimple(int advance = 0)
00042     : CurrentSeed(RandShift + 1), CurrentRand(0) { AdvanceSeed(advance); }
00043 
00044   // copy constructor
00045   RNGSimple(const RNGSimple& rng)
00046     : CurrentSeed(rng.CurrentSeed), CurrentRand(rng.CurrentRand) { }
00047 
00048   // destructor
00049   ~RNGSimple(void) {}
00050 
00051   //   advance indicates number of times to advance random number source
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   // set seed to user-specified value, plus shift to ensure it is large
00059   inline void SetSeed(unsigned long seed) {
00060     CurrentSeed = (seed + RandShift) % RandModulus;
00061     CurrentRand = CurrentSeed;
00062   }
00063 
00064   // get seed value
00065   inline unsigned long GetSeed(void) const { return CurrentSeed; }
00066 
00067   // return the next pseudo-random number (from 0 ... 1)
00068   inline Return_t GetRandom(void) const {
00069     CurrentRand = (CurrentRand * RandMultipplier + RandShift) % RandModulus;
00070     return ( Return_t(CurrentRand) / Return_t(RandModulus) );
00071   }
00072 
00073   // pseudonym for GetRandom()
00074   inline Return_t operator()(void) const { return GetRandom(); }
00075 
00076   // conversion to Return_t, same as GetRandom()
00077   inline operator Return_t() const { return GetRandom(); }
00078 
00079   // return the period of the RNG
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 // A version of SequenceGen with extra constructors to make using this
00095 // class easier.  This is the version that people should use to fill
00096 // containers with a random number sequence in an expression.  This
00097 // class is PETE-aware via its inheritance from SequenceGen.
00098 
00099 class RNGSimpleSequence : public SequenceGen<RNGSimple> {
00100 
00101 public:
00102   // default constructor
00103   RNGSimpleSequence(int advance = 0)
00104     : SequenceGen<RNGSimple>(RNGSimple(advance)) {}
00105 
00106   // copy constructor
00107   RNGSimpleSequence(const RNGSimpleSequence& rngseq)
00108     : SequenceGen<RNGSimple>(rngseq.getGenerator()) {}
00109 
00110   // destructor
00111   ~RNGSimpleSequence(void) {}
00112 
00113   // wrappers around RNG generator functions
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  * $RCSfile: RNGSimple.h,v $   $Author: adelmann $
00127  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00128  * IPPL_VERSION_ID: $Id: RNGSimple.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00129  ***************************************************************************/
00130 

Generated on Mon Jan 16 13:23:59 2006 for IPPL by  doxygen 1.4.6