OPAL (Object Oriented Parallel Accelerator Library) 2022.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
28class RNGXDiv {
29
30public:
31 typedef double Return_t;
32
33public:
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) {
63 SeedUpper = static_cast<double>(long(rijk * INV_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 {
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
97private:
99 mutable double RandLower, RandUpper;
100
101 // advance random number seed for sequence
102 inline void advijk(void) {
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
134public:
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
std::complex< double > a
#define RNG_BASIC_MATH(GEN)
Definition: SequenceGen.h:65
int seed
The current random seed.
Definition: Options.cpp:37
static const double INV_SQR_RANMAX
Definition: RNGXDiv.h:115
~RNGXDiv(void)
Definition: RNGXDiv.h:49
static const double RandMultUpper
Definition: RNGXDiv.h:119
void advijk(void)
Definition: RNGXDiv.h:102
RNGXDiv(int advance=0)
Definition: RNGXDiv.h:35
static const double INV_RANMAX
Definition: RNGXDiv.h:116
static const double FirstSeed
Definition: RNGXDiv.h:121
static const double SeedMultLower
Definition: RNGXDiv.h:118
static const double SeedMultUpper
Definition: RNGXDiv.h:117
static const double RandMultLower
Definition: RNGXDiv.h:120
double SeedLower
Definition: RNGXDiv.h:98
static const double SQR_RANMAX
Definition: RNGXDiv.h:114
double RandUpper
Definition: RNGXDiv.h:99
double Return_t
Definition: RNGXDiv.h:31
double SeedUpper
Definition: RNGXDiv.h:98
static const double RANDOM_MAX
Definition: RNGXDiv.h:113
Return_t GetRandom(void) const
Definition: RNGXDiv.h:79
unsigned long GetSeed(void) const
Definition: RNGXDiv.h:71
double RandLower
Definition: RNGXDiv.h:99
RNGXDiv(const RNGXDiv &rng)
Definition: RNGXDiv.h:44
void SetSeed(unsigned long seed)
Definition: RNGXDiv.h:61
Return_t operator()(void) const
Definition: RNGXDiv.h:89
static Return_t GetRandMax(void)
Definition: RNGXDiv.h:95
void AdvanceSeed(int advance=0)
Definition: RNGXDiv.h:52
Return_t GetRandom(void)
Definition: RNGXDiv.h:150
Return_t operator()(void)
Definition: RNGXDiv.h:151
~RNGXDivSequence(void)
Definition: RNGXDiv.h:144
RNGXDivSequence(int advance=0)
Definition: RNGXDiv.h:136
static Return_t GetRandMax(void)
Definition: RNGXDiv.h:152
void SetSeed(unsigned long seed)
Definition: RNGXDiv.h:148
RNGXDivSequence(const RNGXDivSequence &rngseq)
Definition: RNGXDiv.h:140
unsigned long GetSeed(void) const
Definition: RNGXDiv.h:149
void AdvanceSeed(int adv=0)
Definition: RNGXDiv.h:147
GT::Return_t Return_t
Definition: SequenceGen.h:89