-
Notifications
You must be signed in to change notification settings - Fork 1
/
Random.hpp
147 lines (114 loc) · 4.64 KB
/
Random.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// Random.hpp
// TheBacktrackingSurveyPropagation
//
// Created by Raffaele Marino on 02/12/2018.
// Copyright © 2018 Raffaele Marino. All rights reserved.
//
#ifndef Random_h
#define Random_h
/*Class Random Using WELL Algorithm, minimal modifiications for compatibility*/
/***********************************************************************************/
/***********************************************************************************/
/***********************************************************************************/
/***************************** START RANDOM CLASS **********************************/
/***********************************************************************************/
/***********************************************************************************/
/***********************************************************************************/
/* ***************************************************************************** */
/* Copyright: Francois Panneton and Pierre L'Ecuyer, University of Montreal */
/* Makoto Matsumoto, Hiroshima University */
/* Notice: This code can be used freely for personal, academic, */
/* or non-commercial purposes. For commercial purposes, */
/* please contact P. L'Ecuyer at: lecuyer@iro.UMontreal.ca */
/* ***************************************************************************** */
#define W 32
#define R 32
#define M1 3
#define M2 24
#define M3 10
#define MAT0POS(t,v) (v^(v>>t))
#define MAT0NEG(t,v) (v^(v<<(-(t))))
#define Identity(v) (v)
#define FACT 2.32830643653869628906e-10
static unsigned int state_i = 0;
static unsigned int STATE[R];
static unsigned int z0, z1, z2;
#define V0 STATE[state_i ]
#define VM1 STATE[(state_i+M1) & 0x0000001fU]
#define VM2 STATE[(state_i+M2) & 0x0000001fU]
#define VM3 STATE[(state_i+M3) & 0x0000001fU]
#define VRm1 STATE[(state_i+31) & 0x0000001fU]
#define newV0 STATE[(state_i+31) & 0x0000001fU]
#define newV1 STATE[state_i ]
#define INITLEN 32
class Random{
public:
/*declaration public members for the Random.hpp*/
Random(){
WellRandomInitialization();
};
~Random(){};
void print_seed();
void print_seeds_on_file();
double random_number(){return WELLRNG1024a();};
unsigned int seed();
Random & operator=(Random &myrand){
return *this;
}
private:
/*declaration private variables for the Random.hpp*/
unsigned int _seed;
long int _NSITES;
vector<unsigned int> _vecinittoprint;
void InitWELLRNG1024a (unsigned int *init);
double WELLRNG1024a (void);
unsigned int GetRandom();
void WellRandomInitialization();
};
inline void Random::print_seed(){cout<<_seed<<endl;}
inline unsigned int Random::seed(){return _seed;}
inline void Random::InitWELLRNG1024a (unsigned int *init){
int j;
state_i = 0;
for (j = 0; j < R; j++)
STATE[j] = init[j];
}
inline double Random::WELLRNG1024a (void){
z0 = VRm1;
z1 = Identity(V0) ^ MAT0POS (8, VM1);
z2 = MAT0NEG (-19, VM2) ^ MAT0NEG(-14,VM3);
newV1 = z1 ^ z2;
newV0 = MAT0NEG (-11,z0) ^ MAT0NEG(-7,z1) ^ MAT0NEG(-13,z2) ;
state_i = (state_i + 31) & 0x0000001fU;
return ((double) STATE[state_i] * FACT);
}
inline unsigned int Random::GetRandom(){/*inizialzation seed*/
unsigned int ris;
FILE *devran = fopen("/dev/urandom","r");
fread(&ris, 4, 1, devran);
fclose(devran);
return ris;
}
inline void Random::WellRandomInitialization(){
_seed=GetRandom();
_seed=3549654995;
if (_seed < 1) exit(-1);
srandom(_seed);
unsigned int init[INITLEN];
double j=0;
for (int i=0;i<INITLEN;++i) {
init[i] = (unsigned int) random();
_vecinittoprint.push_back(init[i]);
}
InitWELLRNG1024a(init);
for (int i=0;i<100000;++i) j = WELLRNG1024a();
}
/***********************************************************************************/
/***********************************************************************************/
/***********************************************************************************/
/***************************** END RANDOM CLASS **********************************/
/***********************************************************************************/
/***********************************************************************************/
/***********************************************************************************/
#endif /* Random_h */