QDP++
qdp_rannyu.cc
Go to the documentation of this file.
1
15
16#include "qdp_rannyu.h"
17#include "qdp.h"
18
19namespace QDP
20{
21 namespace RANNYU
22 {
23 namespace
24 {
25 int m[4] = {0, 1, 3513, 821};
26
27 multi1d<int> ran_seed;
28 int __default_seed[4] = {13, 15, 1, 17};
29 bool inited = false;
30
31 double twom12 = 1/4096.0;
32 }
33
34 // Hide the actual RNG
35 namespace
36 {
37 void __rand(double& ran, multi1d<int>& ll)
38 {
39 int ii[4];
40
41 ii[0] = ll[0]*m[3] + ll[1]*m[2] + ll[2]*m[1] + ll[3]*m[0];
42 ii[1] = ll[1]*m[3] + ll[2]*m[2] + ll[3]*m[1];
43 ii[2] = ll[2]*m[3] + ll[3]*m[2];
44 ii[3] = ll[3]*m[3] + 1;
45 ll[3] = ii[3] & 4095;
46 ii[2] = ii[2] + (ii[3] >> 12);
47 ll[2] = ii[2] & 4095;
48 ii[1] = ii[1] + (ii[2] >> 12);
49 ll[1] = ii[1] & 4095;
50 ll[0] = (ii[0] + (ii[1] >> 12)) >> 12;
51 ran = twom12*((double)ll[0] + twom12*((double)ll[1] + twom12*((double)ll[2] + twom12*((double)ll[3]))));
52 }
53 }
54
55
57 double random()
58 {
59 double ran;
60
61 // Initialize the global seed if needed
62 if (! inited)
63 {
64 ran_seed.resize(4);
65 for(int i=0; i < 4; ++i)
66 ran_seed[i] = __default_seed[i];
67
68 inited = true;
69 }
70
71 __rand(ran, ran_seed);
72 return ran;
73 }
74
75
77 void setrn(const multi1d<int>& iseed)
78 {
79 if (iseed.size() != 4)
80 {
81 QDPIO::cerr << __func__ << ": rannyu seed is not length 4\n";
82 QDP_abort(1);
83 }
84
85 ran_seed = iseed;
86 }
87
88
91 {
92 return ran_seed;
93 }
94
95
96 // The RNG, but packaging all the state with it. No side effects.
97 void random(RNGState_t& ran_state)
98 {
99 if (ran_state.seed.size() != 4)
100 {
101 QDPIO::cerr << __func__ << ": rannyu seed is not length 4\n";
102 QDP_abort(1);
103 }
104
105 __rand(ran_state.ran, ran_state.seed);
106 }
107
108 } // namespace RANNYU
109
110} // namespace QDP
111
Container for a multi-dimensional 1D array.
Definition qdp_multi.h:25
int size() const
Size of array.
Definition qdp_multi.h:60
StandardOutputStream cerr
Definition qdp_stdio.cc:22
double random()
The RNG. Has side effects.
Definition qdp_rannyu.cc:57
multi1d< int > savern()
Recover the seed.
Definition qdp_rannyu.cc:90
void setrn(const multi1d< int > &iseed)
Seed has been set by default - this allows one to override it.
Definition qdp_rannyu.cc:77
Yet another random number generator.
void QDP_abort(int status)
Panic button.
Primary include file for QDP.
Yet another random number generator.
Hold state and RNG.
Definition qdp_rannyu.h:31
multi1d< int > seed
Definition qdp_rannyu.h:32