XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ranx.hpp
Go to the documentation of this file.
1 #ifndef RANX_HPP
2 #define RANX_HPP
3 #include<vector>
4 #include <bitset>
5 /* Thread-safe random number generator */
6 namespace Random{
7 
8  // 3 ints store the state
9  struct rands{
10  int s[3];
11  };
12 
14  if ((sv.s[0] & -2) == 0) sv.s[0] -= 1023;
15  if ((sv.s[1] & -8) == 0) sv.s[1] -= 1023;
16  if ((sv.s[2] & -16) == 0) sv.s[2] -= 1023;
17  }
18 
19  double taus88_ext(rands& sv){
20  // Generates a random number between 0 and 1.
21  // Reference:
22  // L'Ecuyer, P. (1996) `Maximally equidistributed combined Tausworthe
23  // generators', Math. of Comput., 65, 203-213.
24 
25  // The cycle length is claimed to be about 2^(88) or about 3E+26.
26  // Actually - (2^31 - 1).(2^29 - 1).(2^28 - 1).
27 
28  uint32_t i1 = sv.s[0];
29  uint32_t i2 = sv.s[1];
30  uint32_t i3 = sv.s[2];
31  uint32_t b = (((i1 << 13) ^ i1) >> 19);
32 
33  i1 = (((i1 & -2) << 12) ^ b);
34  b = (((i2 << 2) ^ i2) >> 25);
35  i2 = (((i2 & -8) << 4) ^ b);
36  b = (((i3 << 3) ^ i3) >> 11);
37  i3 = (((i3 & -16) << 17) ^ b);
38  sv.s[0] = i1;
39  sv.s[1] = i2;
40  sv.s[2] = i3;
41 
42  return int( (i1 ^ i2) ^ i3) * 2.3283064365E-10 + 0.5;
43  }
44 
45  // Global random number pool
46  std::vector<rands> sv;
47 
48  // Initialize seed
49  void init_ranx(int nthreads, int mype){
50  // Allocate
51  sv = std::vector<rands>(nthreads);
52 
53  // Set initial seeds
54  for (int i = 0; i<nthreads; i++){
55  int gi = mype*nthreads + i;
56  sv[i].s[0] = 1234*gi;
57  sv[i].s[1] = 2345*gi + 6789;
58  sv[i].s[2] = 4321*gi + 10;
59  init_seeds_ext( sv[i] );
60  }
61  }
62 
63  // random number generator
64  double ranx(int thread_id){
65  return taus88_ext( sv[thread_id] );
66  }
67 }
68 
69 #endif
double ranx(int thread_id)
Definition: ranx.hpp:64
std::vector< rands > sv
Definition: ranx.hpp:46
void init_seeds_ext(rands &sv)
Definition: ranx.hpp:13
void init_ranx(int nthreads, int mype)
Definition: ranx.hpp:49
Definition: ranx.hpp:9
int s[3]
Definition: ranx.hpp:10
double taus88_ext(rands &sv)
Definition: ranx.hpp:19