OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
RNGSimple.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef RNG_SIMPLE_H
12 #define RNG_SIMPLE_H
13 
14 /***********************************************************************
15  *
16  * class RNGSimple
17  * class RNGSimpleSequence : public SequenceGen<RNGSimple>
18  *
19  * Simple class that implements random number generator a la
20  * Numerical Recipes, in the range [0...1]. The first class may be
21  * used standalone, or as a template parameter to SequenceGen. The
22  * second class is derived from SequenceGen, and makes it easier to
23  * use this RNG in expressions.
24  * Use RNGSimple as a scalar or container element, and use
25  * RNGSimpleSequence when you need a sequence of numbers to fill a container.
26  *
27  ***********************************************************************/
28 
29 // include files
30 #include "Utility/SequenceGen.h"
31 
32 #ifdef IPPL_USE_SINGLE_PRECISION
33  #define CTYPE float
34 #else
35  #define CTYPE double
36 #endif
37 
38 class RNGSimple {
39 
40 public:
41  // return type
42  typedef CTYPE Return_t;
43 
44 public:
45  // default constructor
46  RNGSimple(int advance = 0)
47  : CurrentSeed(RandShift + 1), CurrentRand(0) { AdvanceSeed(advance); }
48 
49  // copy constructor
50  RNGSimple(const RNGSimple& rng)
52 
53  // destructor
54  ~RNGSimple(void) {}
55 
56  // advance indicates number of times to advance random number source
57  inline void AdvanceSeed(int advance = 0) {
58  for (int iadv=0; iadv<advance; iadv++)
61  }
62 
63  // set seed to user-specified value, plus shift to ensure it is large
64  inline void SetSeed(unsigned long seed) {
65  CurrentSeed = (seed + RandShift) % RandModulus;
67  }
68 
69  // get seed value
70  inline unsigned long GetSeed(void) const { return CurrentSeed; }
71 
72  // return the next pseudo-random number (from 0 ... 1)
73  inline Return_t GetRandom(void) const {
76  }
77 
78  // pseudonym for GetRandom()
79  inline Return_t operator()(void) const { return GetRandom(); }
80 
81  // conversion to Return_t, same as GetRandom()
82  inline operator Return_t() const { return GetRandom(); }
83 
84  // return the period of the RNG
85  static Return_t GetRandMax(void) { return Return_t(RandModulus); }
86 
87 private:
89  mutable long CurrentRand;
90 
91  static const long RandModulus;
92  static const long RandMultipplier;
93  static const long RandShift;
94 };
95 
97 
98 
99 // A version of SequenceGen with extra constructors to make using this
100 // class easier. This is the version that people should use to fill
101 // containers with a random number sequence in an expression. This
102 // class is PETE-aware via its inheritance from SequenceGen.
103 
105 
106 public:
107  // default constructor
108  RNGSimpleSequence(int advance = 0)
109  : SequenceGen<RNGSimple>(RNGSimple(advance)) {}
110 
111  // copy constructor
113  : SequenceGen<RNGSimple>(rngseq.getGenerator()) {}
114 
115  // destructor
117 
118  // wrappers around RNG generator functions
119  inline void AdvanceSeed(int adv = 0) { getGenerator().AdvanceSeed(adv); }
120  inline void SetSeed(unsigned long seed) { getGenerator().SetSeed(seed); }
121  inline unsigned long GetSeed(void) const { return getGenerator().GetSeed(); }
122  inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
123  inline Return_t operator()(void) { return getGenerator().GetRandom(); }
124  static Return_t GetRandMax(void) { return RNGSimple::GetRandMax(); }
125 };
126 
127 
128 #endif // RNG_SIMPLE_H
129 
130 /***************************************************************************
131  * $RCSfile: RNGSimple.h,v $ $Author: adelmann $
132  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
133  * IPPL_VERSION_ID: $Id: RNGSimple.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
134  ***************************************************************************/
135 
int seed
The current random seed.
Definition: Options.cpp:41
#define CTYPE
Definition: RNGSimple.h:35
static const long RandShift
Definition: RNGSimple.h:93
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
Return_t GetRandom(void)
Definition: RNGSimple.h:122
Return_t operator()(void)
Definition: RNGSimple.h:123
long CurrentRand
Definition: RNGSimple.h:89
static Return_t GetRandMax(void)
Definition: RNGSimple.h:85
void AdvanceSeed(int adv=0)
Definition: RNGSimple.h:119
void SetSeed(unsigned long seed)
Definition: RNGSimple.h:64
static const long RandModulus
Definition: RNGSimple.h:91
RNGSimple::Return_t Return_t
Definition: SequenceGen.h:89
static Return_t GetRandMax(void)
Definition: RNGSimple.h:124
RNGSimpleSequence(const RNGSimpleSequence &rngseq)
Definition: RNGSimple.h:112
RNGSimpleSequence(int advance=0)
Definition: RNGSimple.h:108
~RNGSimple(void)
Definition: RNGSimple.h:54
RNGSimple(const RNGSimple &rng)
Definition: RNGSimple.h:50
static const long RandMultipplier
Definition: RNGSimple.h:92
unsigned long GetSeed(void) const
Definition: RNGSimple.h:121
double Return_t
Definition: RNGSimple.h:42
Return_t operator()(void) const
Definition: RNGSimple.h:79
unsigned long GetSeed(void) const
Definition: RNGSimple.h:70
Return_t GetRandom(void) const
Definition: RNGSimple.h:73
RNGSimple(int advance=0)
Definition: RNGSimple.h:46
void AdvanceSeed(int advance=0)
Definition: RNGSimple.h:57
~RNGSimpleSequence(void)
Definition: RNGSimple.h:116
long CurrentSeed
Definition: RNGSimple.h:88
void SetSeed(unsigned long seed)
Definition: RNGSimple.h:120