OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
RNGXDiv.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  ***************************************************************************/
7 
8 #ifndef RNG_XDIV_H
9 #define RNG_XDIV_H
10 
11 /***********************************************************************
12  *
13  * class RNGXDiv
14  * class RNGXDivSequence : public SequenceGen<RNGXDiv>
15  *
16  * Simple class that implements random number generator from LANL
17  * X-Division folks, in the range [0...1]. The first class may be
18  * used standalone, or as a template parameter to SequenceGen. The
19  * second class is derived from SequenceGen, and makes it easier to
20  * use this RNG in expressions.
21  * Use RNGXDiv as a scalar or container element, and use
22  * RNGXDivSequence when you need a sequence of numbers to fill a container.
23  *
24  ***********************************************************************/
25 
26 #include "Utility/SequenceGen.h"
27 
28 class RNGXDiv {
29 
30 public:
31  typedef double Return_t;
32 
33 public:
34  // default constructor
35  RNGXDiv(int advance = 0) {
36  // set the first random number, composed of
37  // SeedUpper (top 24 bits) and SeedLower (bottom 24 bits).
38  SeedUpper = static_cast<double>(long(FirstSeed * INV_SQR_RANMAX));
40  AdvanceSeed(advance); // advance the seed
41  }
42 
43  // copy constructor
44  RNGXDiv(const RNGXDiv& rng)
47 
48  // destructor
49  ~RNGXDiv(void) {}
50 
51  // advance indicates number of times to advance random number source
52  inline void AdvanceSeed(int advance = 0) {
53  for (int iadv=0; iadv<advance; iadv++)
54  advijk();
55  // set sequence to new source
58  }
59 
60  // set seed to user-specified value, plus shift to ensure it is large
61  inline void SetSeed(unsigned long seed) {
62  Return_t rijk = Return_t(seed) + FirstSeed;
63  SeedUpper = static_cast<double>(long(rijk * INV_SQR_RANMAX));
64  SeedLower = rijk - SeedUpper * SQR_RANMAX;
65  // set sequence to new source
68  }
69 
70  // get seed value
71  inline unsigned long GetSeed(void) const {
72  // invert process for setting seed
74  unsigned long seed = (unsigned long) (rijk - FirstSeed);
75  return seed;
76  }
77 
78  // return the next pseudo-random number
79  inline Return_t GetRandom(void) const {
81  Return_t b = RandMultUpper * RandLower +
83  RandLower = a - long(a * INV_SQR_RANMAX) * SQR_RANMAX;
84  RandUpper = b - long(b * INV_SQR_RANMAX) * SQR_RANMAX;
85  return ( (RandUpper * SQR_RANMAX + RandLower) * INV_RANMAX );
86  }
87 
88  // pseudonym for GetRandom()
89  inline Return_t operator()(void) const { return GetRandom(); }
90 
91  // conversion to Return_t, same as GetRandom()
92  inline operator Return_t() const { return GetRandom(); }
93 
94  // return the period of the RNG
95  static Return_t GetRandMax(void) { return Return_t(RANDOM_MAX); }
96 
97 private:
99  mutable double RandLower, RandUpper;
100 
101  // advance random number seed for sequence
102  inline void advijk(void) {
104  Return_t b = (SeedMultUpper * SeedLower -
105  long(SeedMultUpper * SeedLower * INV_SQR_RANMAX) * SQR_RANMAX) +
107  long(SeedMultLower * SeedUpper * INV_SQR_RANMAX) * SQR_RANMAX) +
108  long(a * INV_SQR_RANMAX);
109  SeedLower = a - long(a * INV_SQR_RANMAX) * SQR_RANMAX;
110  SeedUpper = b - long(b * INV_SQR_RANMAX) * SQR_RANMAX;
111  }
112 
113  static const double RANDOM_MAX;
114  static const double SQR_RANMAX;
115  static const double INV_SQR_RANMAX;
116  static const double INV_RANMAX;
117  static const double SeedMultUpper;
118  static const double SeedMultLower;
119  static const double RandMultUpper;
120  static const double RandMultLower;
121  static const double FirstSeed;
122 };
123 
125 
126 
127 // A version of SequenceGen with extra constructors to make using this
128 // class easier. This is the version that people should use to fill
129 // containers with a random number sequence in an expression. This
130 // class is PETE-aware via its inheritance from SequenceGen.
131 
133 
134 public:
135  // default constructor
136  RNGXDivSequence(int advance = 0)
137  : SequenceGen<RNGXDiv>(RNGXDiv(advance)) {}
138 
139  // copy constructor
141  : SequenceGen<RNGXDiv>(rngseq.getGenerator()) {}
142 
143  // destructor
145 
146  // wrappers around RNG generator functions
147  inline void AdvanceSeed(int adv = 0) { getGenerator().AdvanceSeed(adv); }
148  inline void SetSeed(unsigned long seed) { getGenerator().SetSeed(seed); }
149  inline unsigned long GetSeed(void) const { return getGenerator().GetSeed(); }
150  inline Return_t GetRandom(void) { return getGenerator().GetRandom(); }
151  inline Return_t operator()(void) { return getGenerator().GetRandom(); }
152  static Return_t GetRandMax(void) { return RNGXDiv::GetRandMax(); }
153 };
154 
155 
156 #endif // RNG_XDIV_H
157 
double Return_t
Definition: RNGXDiv.h:31
int seed
The current random seed.
Definition: Options.cpp:37
double RandUpper
Definition: RNGXDiv.h:99
static Return_t GetRandMax(void)
Definition: RNGXDiv.h:95
static const double RANDOM_MAX
Definition: RNGXDiv.h:113
~RNGXDiv(void)
Definition: RNGXDiv.h:49
static const double SeedMultUpper
Definition: RNGXDiv.h:117
static const double SQR_RANMAX
Definition: RNGXDiv.h:114
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
RNGXDivSequence(const RNGXDivSequence &rngseq)
Definition: RNGXDiv.h:140
double SeedUpper
Definition: RNGXDiv.h:98
double SeedLower
Definition: RNGXDiv.h:98
static const double INV_RANMAX
Definition: RNGXDiv.h:116
static const double RandMultUpper
Definition: RNGXDiv.h:119
void SetSeed(unsigned long seed)
Definition: RNGXDiv.h:61
static const double FirstSeed
Definition: RNGXDiv.h:121
RNGXDivSequence(int advance=0)
Definition: RNGXDiv.h:136
unsigned long GetSeed(void) const
Definition: RNGXDiv.h:149
static const double SeedMultLower
Definition: RNGXDiv.h:118
RNGXDiv::Return_t Return_t
Definition: SequenceGen.h:89
~RNGXDivSequence(void)
Definition: RNGXDiv.h:144
void advijk(void)
Definition: RNGXDiv.h:102
double RandLower
Definition: RNGXDiv.h:99
RNGXDiv(int advance=0)
Definition: RNGXDiv.h:35
static const double RandMultLower
Definition: RNGXDiv.h:120
Return_t operator()(void) const
Definition: RNGXDiv.h:89
static Return_t GetRandMax(void)
Definition: RNGXDiv.h:152
void SetSeed(unsigned long seed)
Definition: RNGXDiv.h:148
Return_t GetRandom(void) const
Definition: RNGXDiv.h:79
void AdvanceSeed(int adv=0)
Definition: RNGXDiv.h:147
unsigned long GetSeed(void) const
Definition: RNGXDiv.h:71
Return_t operator()(void)
Definition: RNGXDiv.h:151
we sometimes make exceptions for this Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally NO WARRANTY BECAUSE THE PROGRAM IS LICENSED FREE OF THERE IS NO WARRANTY FOR THE TO THE EXTENT PERMITTED BY APPLICABLE LAW EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND OR OTHER PARTIES PROVIDE THE PROGRAM AS IS WITHOUT WARRANTY OF ANY EITHER EXPRESSED OR BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU SHOULD THE PROGRAM PROVE YOU ASSUME THE COST OF ALL NECESSARY REPAIR OR CORRECTION IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT OR ANY OTHER PARTY WHO MAY MODIFY AND OR REDISTRIBUTE THE PROGRAM AS PERMITTED BE LIABLE TO YOU FOR INCLUDING ANY INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new and you want it to be of the greatest possible use to the public
Definition: LICENSE:284
RNGXDiv(const RNGXDiv &rng)
Definition: RNGXDiv.h:44
static const double INV_SQR_RANMAX
Definition: RNGXDiv.h:115
Return_t GetRandom(void)
Definition: RNGXDiv.h:150
void AdvanceSeed(int advance=0)
Definition: RNGXDiv.h:52