OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
RNGRand.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_RAND_H
12 #define RNG_RAND_H
13 
14 /***********************************************************************
15  *
16  * class RNGRand
17  * class RNGRandSequence : public SequenceGen<RNGRand>
18  *
19  * Simple wrapper around C rand() function, to generate random numbers
20  * 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 RNGRand as a scalar or container element, and use
25  * RNGRandSequence 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 #include <cstdlib>
39 
40 class RNGRand {
41 
42 public:
43  // return type
44  typedef CTYPE Return_t;
45 
46 public:
47  // default constructor
48  RNGRand(int advance = 0) : CurrentSeed(1U) { AdvanceSeed(advance); }
49 
50  // copy constructor
51  RNGRand(const RNGRand& rng) : CurrentSeed(rng.CurrentSeed) {}
52 
53  // destructor
54  ~RNGRand(void) {}
55 
56  // advance indicates number of times to advance random number source
57  // there's no standard way to do this with rand(), so make one up!
58  // just require one-to-one correspondence of old and new seeds
59  inline void AdvanceSeed(int advance = 0) {
60  for (int iadv=0; iadv<advance; iadv++) {
61  // first reseed with current seed
62  srand(CurrentSeed);
63  // use first random number as seed step
64  int seedstep = rand();
65  CurrentSeed = (CurrentSeed + seedstep) % RAND_MAX;
66  }
67  // finally, reseed with new seed value
68  srand(CurrentSeed);
69  }
70 
71  // set seed to user-specified value (any shifting is done by srand)
72  inline void SetSeed(unsigned int seed) {
73  CurrentSeed = seed;
74  srand(CurrentSeed);
75  }
76 
77  // get seed value
78  inline unsigned int GetSeed(void) const { return CurrentSeed; }
79 
80  // return the next pseudo-random number (from 0 ... 1)
81  inline Return_t GetRandom(void) const {
82  return ( (Return_t(rand())) / (Return_t(RAND_MAX)+1) );
83  }
84 
85  // pseudonym for GetRandom()
86  inline Return_t operator()(void) const { return GetRandom(); }
87 
88  // conversion to Return_t, same as GetRandom()
89  inline operator Return_t() const { return GetRandom(); }
90 
91  // return the period of the RNG
92  static Return_t GetRandMax(void) { return Return_t(RAND_MAX)+1; }
93 
94 private:
95  unsigned int CurrentSeed;
96 };
97 
99 
100 
101 // A version of SequenceGen with extra constructors to make using this
102 // class easier. This is the version that people should use to fill
103 // containers with a random number sequence in an expression. This
104 // class is PETE-aware via its inheritance from SequenceGen.
105 
107 
108 public:
109  // default constructor
110  RNGRandSequence(int advance = 0)
111  : SequenceGen<RNGRand>(RNGRand(advance)) {}
112 
113  // copy constructor
115  : SequenceGen<RNGRand>(rngseq.getGenerator()) {}
116 
117  // destructor
119 
120  // wrappers around RNG generator functions
121  inline void AdvanceSeed(int adv = 0) { getGenerator().AdvanceSeed(adv); }
122  inline void SetSeed(unsigned int seed) { getGenerator().SetSeed(seed); }
123  inline unsigned int GetSeed(void) const { return getGenerator().GetSeed(); }
124  inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
125  inline Return_t operator()(void) { return getGenerator().GetRandom(); }
126  static Return_t GetRandMax(void) { return RNGRand::GetRandMax(); }
127 };
128 
129 
130 #endif // RNG_RAND_H
131 
132 /***************************************************************************
133  * $RCSfile: RNGRand.h,v $ $Author: adelmann $
134  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
135  * IPPL_VERSION_ID: $Id: RNGRand.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
136  ***************************************************************************/
137 
int seed
The current random seed.
Definition: Options.cpp:41
RNGRandSequence(int advance=0)
Definition: RNGRand.h:110
RNGRand(const RNGRand &rng)
Definition: RNGRand.h:51
unsigned int CurrentSeed
Definition: RNGRand.h:95
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
void AdvanceSeed(int adv=0)
Definition: RNGRand.h:121
static Return_t GetRandMax(void)
Definition: RNGRand.h:126
RNGRandSequence(const RNGRandSequence &rngseq)
Definition: RNGRand.h:114
RNGRand::Return_t Return_t
Definition: SequenceGen.h:89
void AdvanceSeed(int advance=0)
Definition: RNGRand.h:59
unsigned int GetSeed(void) const
Definition: RNGRand.h:123
Return_t operator()(void)
Definition: RNGRand.h:125
#define CTYPE
Definition: RNGRand.h:35
static Return_t GetRandMax(void)
Definition: RNGRand.h:92
Return_t GetRandom(void)
Definition: RNGRand.h:124
Return_t GetRandom(void) const
Definition: RNGRand.h:81
Return_t operator()(void) const
Definition: RNGRand.h:86
RNGRand(int advance=0)
Definition: RNGRand.h:48
double Return_t
Definition: RNGRand.h:44
void SetSeed(unsigned int seed)
Definition: RNGRand.h:72
~RNGRand(void)
Definition: RNGRand.h:54
unsigned int GetSeed(void) const
Definition: RNGRand.h:78
void SetSeed(unsigned int seed)
Definition: RNGRand.h:122
~RNGRandSequence(void)
Definition: RNGRand.h:118