-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt.hpp
308 lines (221 loc) · 6.22 KB
/
mt.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//Header file for a basic random number generator-seeded by random_device → <random.cpp>
//Also includes Class Distribution
#define __MT19937_RAND_DEFAULT_WARMUP__ 700000
//Reference - "Improved long-period generators based on linear recurrences modulo 2", F. Panneton, P. L'Ecuyer, M. Matsumoto in AVM TOMS Volume 32 Issue 1, March 2006 Pages 1-16
using Rnd_Wrapper<std::mt19937>(__MT19937_RAND_DEFAULT_WARMUP__) = MT_RND;
#pragma once
#include "urandom.h"
#include <random>
#include <chrono>
#include <sstream>
#include <fstream>
#define __MT19937_RAND_DEFAULT_WARMUP__ 700000
//Reference - "Improved long-period generators based on linear recurrences modulo 2", F. Panneton, P. L'Ecuyer, M. Matsumoto in AVM TOMS Volume 32 Issue 1, March 2006 Pages 1-16
//RND Return Codes
enum class RND_RC : int
{
Success = 0,
EntropyShortage = 1,
SeedNotFound = 2,
FileError = -1
};
//Extern function
const char* RND_RC_Str(RND_RC x)
{
if (x == RND_RC::AllSuccess)
return "AllSuccess";
else if(x == RND_RC::EntropyShortage)
return "EntropyShortage";
else if(x == RND_RC::SeedNotFound)
return "SeedNotFound";
} // End of RND_RC_Str()
class MT_RND
{
private:
std::vector<unsigned int> seed_list; //Store Seds
bool warmed_up;
size_t discard_length; //Discard Length
Urandom urandom; //Instantiate with unsigned int type
public:
std::mt19937 engine; //Random MT Engine
//std::string name; //Optional Name given to the engine
//Constructor
MT_RND(/*std::string name = "",*/
) : /*name(name),*/ /*discard_length(discard),*/ warmed_up(false)
{
discard_length = __MT19937_RAND_DEFAULT_WARMUP__ ;
//Empty Body --> Allows more complex operations.
}
void TimeSeed() //Currently Only Supports the current time.
{
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
SingleSeed(seed);
}
void SingleSeed(unsigned int x_seed)
{
#if SpS_COMPILETIME_WARNINGS == 1
static_assert(false, "[WARNING] MT_RND::SingleSeed() produes low entropy - low period engines.\n");
#endif
//Free-up vector
if(!seed_list.empty())
{
FreeSeedMem();
}
seed_list.emplace_back(x_seed); //Write the seed
engine.seed(x_seed);
}
//Specific Construction of Seed List vector??
//Needs to be explicitly called
size_t Discard(size_t discard_arg = 0, const char* str_t = "")
{
if((discard_length == 0 && discard_arg == 0) || str_t == "any")
{
//Discard a random length
urandom.Open();
size_t advance = urandom.get<unsigned int>();
//!!! size_t is not the template arguement for urandom class
discard_length += advance; //Adds to the discard_count
engine.discard(advance);
return advance;
}
else if(discard_arg != 0)
{
engine.discard(discard_arg);
discard_length += discard_arg;
return discard_arg;
}
//Repeated Invocations will keep discarding equal to discard_length
else if(discard_length !=0) //Can be invoked in the case of an Old Seed
{
engine.discard(discard_length);
if(warmed_up)
discard_length += discard_length;
return discard_length;
}
else
return 0;
}
std::vector<unsigned int> getSeedList()
{
return seed_list;
}
unsigned int FirstSeed() const
{
if(seed_list.size() > 0)
return seed_list[0];
else
return 0.0;
}
RND_RC NewSeeds()
{
// Magic number 624: The number of unsigned ints the MT uses as state
//std::array<std::mt19937::state_size, unsigned int> rnd_seed_array;
seed_list.reserve(std::mt19937::state_size);
//Open Urandom_handler -> Instantiate
urandom.Open();
//Read Sequentially
for(unsigned int i = 0; i < std::mt19937::state_size; i++)
{
seed_list.emplace_back(urandom.get<unsigned int>());
}
//std::generate(begin(seed_list), end(seed_list), urandom.get());
urandom.Close();
std::seed_seq seeds(std::begin(seed_list), std::end(seed_list));
engine.seed(seeds);
engine.discard(discard_length);
warmed_up = true;
return RND_RC::AllSuccess;
} //End of NewSeed
//Old Seed Does not Call std::mt19937::discard()
//What is the file-format? JSON, newline seperated value list, Binary?
RND_RC OldSeed(std::string &path_to_seed)
{
// Magic number 624: The number of unsigned ints the MT uses as state
//std::array<std::mt19937::state_size, unsigned int> rnd_seed_array = {0};
seed_list.reserve(std::mt19937::state_size);
std::ifstream seed_file(path_to_seed, std::ios::in); //Open file
if(!seed_file.is_open())
{
return RND_RC::SeedNotFound;
}
//for(unsigned int i = 0; i < std::mt19937::state_size; i++)
int i = 0; unsigned int tmp = 0;
while(seed_file >> tmp && i < std::mt19937::state_size)
{
seed_list.emplace_back(tmp);
i++;
}
std::seed_seq seeds(std::begin(seed_list), std::end(seed_list));
engine.seed(seeds); //PRNG Seeded
if(i < std::mt19937::state_size)
{
return RND_RC::EntropyShortage;
}
else
return RND_RC::AllSuccess;
} //End of Old Seed
//OK
void FreeSeedMem()
{
//Create a temporary vector
std::vector<unsigned int> tmp_vec;
tmp_vec.swap(seed_list); //Swap
//On exit -> seed_list(which is now tmp_vec) is destroyed and freed.
}
//OK
void SaveSeed(const std::string &filename)
{
std::ostringstream buffer;
for(unsigned int i = 0; i < seed_list.size(); i++)
{buffer << seed_list[i] << '\n';}
std::ofstream fout(filename, std::ios::out);
fout << buffer.str();
//RAII fout close expected
}
//Ok
void SaveState(const std::string &filename)
{
std::ofstream fout(filename, std::ios::out);
fout << engine; //Save engine state
//RAII fout close expected
}
//Ok
void LoadState(const std::string &filename)
{
std::ifstream fin(filename, std::ios::in);
fin >> engine; //Save engine state
//RAII fin close expected
}
//Ok
unsigned int SeedSize()
{
return seed_list.size();
}
//OK
//Does not close file
template <typename T>
T get_nondet()
{
if(urandom.is_open())
{
urandom.Open();
}
return urandom.get<T>();
}
//Ok
//Closes urandom after read
template <typename T>
T get_nondet_nclose()
{
//Open
if(!urandom.is_open())
{
urandom.Open();
}
//Read
T tmp = urandom.get<T>();
//Close
urandom.Close();
return tmp;
}
};