src/Utility/RNGBitReverse.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_BIT_REVERSE_H
00012 #define RNG_BIT_REVERSE_H
00013 
00014 /***********************************************************************
00015  * 
00016  * class RNGBitReverse
00017  * class RNGBitReverseSequence : public SequenceGen<RNGBitReverse>
00018  *
00019  * RNGBitReverse generates a set of values distributed over some domain
00020  * based on a bit-reversal algorithm.  These numbers will be uniformly
00021  * distributed over the [0...1] domain.  It requires the following
00022  * parameters and values:
00023  *
00024  * base - used in bit reversal calculation; must be a prime number
00025  * seed - a starting unsigned integer, which is incremented as new values
00026  *        are calculated.  If not specified, it defaults to 1 initially.
00027  *        It is incremented after each calculation.
00028  *
00029  * RNGBitReverseSequence is derived from SequenceGen, and makes it easier
00030  * to use this RNG in expressions.
00031  * Use RNGBitReverse as a scalar or container element, and use
00032  * RNGBitReverseSequence when you need a sequence of numbers to fill
00033  * a container.
00034  *
00035  ***********************************************************************/
00036 
00037 // include files
00038 #include "Utility/SequenceGen.h"
00039 
00040 
00041 class RNGBitReverse {
00042 
00043 public:
00044   // return type
00045   typedef double Return_t;
00046 
00047 public:
00048   // default constructor
00049   RNGBitReverse(unsigned int base = 2, unsigned long seed = 1)
00050     : Base(base), Seed(seed) {}
00051 
00052   // copy constructor
00053   RNGBitReverse(const RNGBitReverse& brg)
00054     : Base(brg.Base), Seed(brg.Seed) {}
00055 
00056   // destructor
00057   ~RNGBitReverse(void) {}
00058 
00059   // advance seed for RNG n times
00060   inline void AdvanceSeed(unsigned long n = 0) { Seed += n; }
00061 
00062   // set seed to specified value
00063   inline void SetSeed(unsigned long seed)        { Seed = seed; }
00064 
00065   // set base to specified value (should be a prime number!)
00066   inline void SetBase(unsigned int base)         { Base = base; }
00067 
00068   // get seed value
00069   inline unsigned long GetSeed(void) const       { return Seed; }
00070 
00071   // get base value
00072   inline unsigned int GetBase(void) const        { return Base; }
00073 
00074   // get the next random number in sequence
00075   inline Return_t GetRandom(void) const {
00076     Return_t rev = 0.0;
00077     Return_t power = 1.0;
00078     long inum = Seed;
00079     while (inum > 0) {
00080       int iquot = inum/Base;
00081       int irem = inum - iquot*Base;
00082       power /= Return_t(Base);
00083       rev += Return_t(irem)*power;
00084       inum = iquot;
00085     }
00086     Seed++;
00087     return rev;
00088   }
00089 
00090   // pseudonym for GetRandom()
00091   inline Return_t operator()(void) const { return GetRandom(); }
00092 
00093   // conversion to Return_t, same as GetRandom()
00094   inline operator Return_t() const { return GetRandom(); }
00095 
00096 private:
00097   // the base and seed for this generator
00098   unsigned int          Base;
00099   mutable unsigned long Seed;
00100 };
00101 
00102 RNG_BASIC_MATH(RNGBitReverse)
00103 
00104 
00105 // A version of SequenceGen with extra constructors to make using this
00106 // class easier.  This is the version that people should use to fill
00107 // containers with a random number sequence in an expression.  This
00108 // class is PETE-aware via its inheritance from SequenceGen.
00109 
00110 class RNGBitReverseSequence : public SequenceGen<RNGBitReverse> {
00111 
00112 public:
00113   // default constructor
00114   RNGBitReverseSequence(unsigned int base = 2, unsigned long seed = 1)
00115     : SequenceGen<RNGBitReverse>(RNGBitReverse(base,seed)) {}
00116 
00117   // copy constructor
00118   RNGBitReverseSequence(const RNGBitReverseSequence& rngseq)
00119     : SequenceGen<RNGBitReverse>(rngseq.getGenerator()) {}
00120 
00121   // destructor
00122   ~RNGBitReverseSequence(void) {}
00123 
00124   // wrappers around RNG generator functions
00125   inline void AdvanceSeed(unsigned long adv = 0) {
00126     getGenerator().AdvanceSeed(adv);
00127   }
00128 
00129   inline void SetSeed(unsigned long seed) { getGenerator().SetSeed(seed); }
00130   inline void SetBase(unsigned int base)  { getGenerator().SetBase(base); }
00131   inline unsigned long GetSeed(void) const { return getGenerator().GetSeed(); }
00132   inline unsigned int GetBase(void) const { return getGenerator().GetBase(); }
00133 
00134   inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
00135   inline Return_t operator()(void) { return getGenerator().GetRandom(); }
00136 };
00137 
00138 #endif // RNG_BIT_REVERSE_H
00139 
00140 /***************************************************************************
00141  * $RCSfile: RNGBitReverse.h,v $   $Author: adelmann $
00142  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00143  * IPPL_VERSION_ID: $Id: RNGBitReverse.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00144  ***************************************************************************/

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