35 #define RC_BASERND_u32_to_f32_divisor 4294967425.0
36 #define RC_BASERND_u64_to_f64_divisor 18446744073709552645.0L
61 constexpr
static inline u32 min() {
return 0; }
64 constexpr
static inline u32 max() {
return 0xFFFFFFFF; }
87 return (
f32(
Get_u32()) / RC_BASERND_u32_to_f32_divisor );
93 return (
f64(
Get_u64()) / RC_BASERND_u64_to_f64_divisor );
101 if (u8_offset >= 32) {
106 retval =
u8(u8_rndstore >> u8_offset);
126 if (u16_offset >= 32) {
131 retval =
u16(u16_rndstore >> u16_offset);
161 return (
Get_f64() < probability);
169 return (
Get_f32() < probability);
201 return Get_f64()*(high-low)+low;
210 for (i=0; i<data.
size(); i++) {
222 template<
class T>
inline static T EntAbs(T x) {
223 return (((x)<0)?(-(x)):(x));
225 inline static void EntRot(
u64 &x,
u32 left_by) {
226 x = (x << left_by) + (x >> (64-left_by));
241 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B,
242 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
243 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
244 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
245 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2,
246 0xEB, 0x27, 0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0,
247 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
248 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
249 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F,
250 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5,
251 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 0xCD, 0x0C, 0x13, 0xEC,
252 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
253 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14,
254 0xDE, 0x5E, 0x0B, 0xDB, 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C,
255 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D,
256 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
257 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F,
258 0x4B, 0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
259 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11,
260 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
261 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F,
262 0xB0, 0x54, 0xBB, 0x16
268 const u32 empirical_entropy_factor = 32;
273 for (
u32 i=0; i < 64*empirical_entropy_factor; i++) {
277 while (EntAbs(
RC::Time::Get() - start_t) <= delta) { loops++; }
278 delta /= 0.98 + 0.04*(loops>0);
280 if (delta < half_prec) { delta = half_prec; }
283 i64 loopdiff = (
i64)loops - (
i64)last_loops;
284 if (loopdiff < 0) { loopdiff = -loopdiff; }
285 loopdiffsum += loopdiff;
293 for (
u32 b=0; b<8; b++) {
295 u64 keep_mask = ~(
u64(0xFFL) << shift);
296 u8 look_up =
u8(entropy >> shift);
297 entropy = (entropy & keep_mask) + (
u64(sbox[look_up]) << shift);
302 if ((i&7) == 0 && loopdiffsum > 64*empirical_entropy_factor) {
306 std::cout <<
"Delta arr: " << delta_arr <<
"\n";
337 for (
size_t i=1; i<MT.
size(); i++) {
338 MT[i] = (1812433253 * (MT[i-1] ^ (MT[i-1] >> 30)) + i);
347 if (init_key.
size() == 0) {
355 end = (624 > init_key.
size()) ? 624 : init_key.
size();
357 for (k=0; k<end; k++) {
358 MT[i] = (MT[i] ^ ((MT[i-1]) ^ (MT[i-1] >> 30)) * 1664525)
369 if (j >= init_key.
size()) {
374 for (k=0; k<623; k++) {
375 MT[i] = (MT[i] ^ ((MT[i-1] ^ (MT[i-1] >> 30)) * 1566083941)) - i;
419 time_seed[0] =
u32(time*1e9);
420 time_seed[1] =
u32(time);
421 time_seed[2] =
u32(entropy);
422 time_seed[3] =
u32(entropy>>32);
433 urand.
Read(init_key);
441 inline void UpdateMT() {
444 u32 xorarr[2] = {0, 0x9908b0df};
447 for (i=0; i<227; i++) {
448 val = (MT[i] & 0x80000000) + (MT[i+1] & 0x7FFFFFFF);
449 MT[i] = MT[i+397] ^ (val>>1);
450 MT[i] ^= xorarr[val & 1];
453 val = (MT[i] & 0x80000000) + (MT[i+1] & 0x7FFFFFFF);
454 MT[i] = MT[i-227] ^ (val>>1);
455 MT[i] ^= xorarr[val & 1];
457 val = (MT[i] & 0x80000000) + (MT[0] & 0x7FFFFFFF);
458 MT[i] = MT[396] ^ (val>>1);
459 MT[i] ^= xorarr[val & 1];
468 if (u32_index >= MT.
size()) {
476 val ^= ((val << 7) & 0x9d2c5680);
477 val ^= ((val << 15) & 0xefc60000);
507 if (u32_offset >= 64) {
512 retval =
u32(u32_rndstore >> u32_offset);
526 #define RC_MINGW_2014_BUGFIX
528 #define RC_MINGW_2014_BUGFIX RC_THREAD_LOCAL
535 #define RC_MAKE_GET_RANGE(classname) \
540 inline u64 classname##_Get_Range(u64 range) {\
541 static RC_MINGW_2014_BUGFIX classname rng;\
542 return rng.GetRange(range);\
558 inline URand(
const size_t buf_size=128)
561 index (urand_buf.size()) {
567 if (index >= urand_buf.
size()) {
568 urand_fr.
Read(urand_buf);
572 return urand_buf[index++];
590 #define RC_MAKE_RND_GEN(classname) \
592 inline classname& classname##_Gen() {\
593 static RC_MINGW_2014_BUGFIX classname rng;\
605 class RC_BaseRND_Float_Bounds_Checker {
612 char RC_Get_f32_Never_Return_One_Check[int((1-0xFFFFFFFF/RC_BASERND_u32_to_f32_divisor)*1e10)];
619 char RC_Get_f64_Never_Return_One_Check[int((1-0xFFFFFFFFFFFFFFFFull/RC_BASERND_u64_to_f64_divisor)*1e20)];
Provides a one-dimensional vector-like structure.
Provides file input and output, and file / directory tools.
Provides a set of convenience macros for metaprogramming and debugging.
#define RC_GetT(T)
Generates wrappers for Get_T functions.
Definition: Macros.h:239
#define RC_DEV_URANDOM
Set to the full pathname of the /dev/urandom file to use.
Definition: RCconfig.h:82
#define RC_MAKE_RND_GEN(classname)
Generates a family of random generator singletons.
Definition: RND.h:587
#define RC_MAKE_GET_RANGE(classname)
Generates a family of RND_Get_Range functions.
Definition: RND.h:535
Provides a robust value-added wrapper for std::string.
Provides classes for accessing and working with dates and times.
Provides typedefs and routines for working with primitives.
uint64_t u64
64-bit unsigned integer.
Definition: Types.h:31
int32_t i32
32-bit signed integer.
Definition: Types.h:28
#define RC_THREAD_LOCAL
Provides thread_local if available.
Definition: Types.h:61
float f32
32-bit float.
Definition: Types.h:33
int64_t i64
64-bit signed integer.
Definition: Types.h:30
uint8_t u8
8-bit unsigned integer.
Definition: Types.h:25
double f64
64-bit float.
Definition: Types.h:34
uint16_t u16
16-bit unsigned integer.
Definition: Types.h:27
int16_t i16
16-bit signed integer.
Definition: Types.h:26
int8_t i8
8-bit signed integer.
Definition: Types.h:24
uint32_t u32
32-bit unsigned integer.
Definition: Types.h:29
An abstract class which provides functions for obtaining randomness in convenient forms.
Definition: RND.h:33
u64 Get_u64()
Provides random u64 values.
Definition: RND.h:74
i8 Get_i8()
Provides random i8 values.
Definition: RND.h:113
bool GetProb(f64 probability)
Returns true with probability, which should be in the range [0,1].
Definition: RND.h:160
i64 Get_i64()
Provides random i64 values.
Definition: RND.h:80
char Get_char()
Provides random char values.
Definition: RND.h:118
i64 GetRange(i64 range)
Like GetRange() but for i64.
Definition: RND.h:183
T GetFrom(Data1D< T > &data)
Returns a random element from the Data1D.
Definition: RND.h:217
bool GetProb(f32 probability)
Returns true with probability, which should be in the range [0,1].
Definition: RND.h:168
T GetRange(T low, T high)
Provides a random integer value in the range [low,high].
Definition: RND.h:197
constexpr static u32 min()
Returns 0 for UniformRandomNumberGenerator compliance.
Definition: RND.h:61
u32 GetRange(u32 range)
Like GetRange() but for i32.
Definition: RND.h:189
u32 result_type
For UniformRandomNumberGenerator compliance.
Definition: RND.h:58
BaseRND()
Default constructor.
Definition: RND.h:42
constexpr static u32 max()
Returns u32 max for UniformRandomNumberGenerator compliance.
Definition: RND.h:64
f64 Get_f64()
Provides a random f64 in the range [0,1).
Definition: RND.h:92
virtual u32 Get_u32()=0
Implement this for all subclasses. All other functions get their randomness from this.
f64 GetFRange(f64 low, f64 high)
Provides a random float in the range [low,high).
Definition: RND.h:200
u8 Get_u8()
Provides random u8 values.
Definition: RND.h:98
u32 operator()()
Equivalent to Get_u32().
Definition: RND.h:55
i32 GetRange(i32 range)
Like GetRange() but for i32.
Definition: RND.h:193
u64 GetRange(u64 range)
Returns [0,range)
Definition: RND.h:179
void Get(u8 &x)
Overloaded function to extract type u8 from this class.
Definition: RND.h:143
void Fill(Data1D< T > &data)
For any supported data type, fills the Data1D with random values.
Definition: RND.h:207
static u64 GetEntropy()
Provides 64 bits of environmental entropy.
Definition: RND.h:238
i16 Get_i16()
Provides random i16 values.
Definition: RND.h:138
u16 Get_u16()
Provides random u16 values.
Definition: RND.h:123
i32 Get_i32()
Provides random i32 values.
Definition: RND.h:68
f32 Get_f32()
Provides a random f32 in the range [0,1).
Definition: RND.h:86
A bounds-safe one-dimensional vector-like structure.
Definition: Data1D.h:49
size_t size() const
Returns the current number of elements.
Definition: Data1D.h:359
Provides true random numbers sourced from environmental noise.
Definition: RND.h:497
EntropyRND()
Default constructor.
Definition: RND.h:501
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:504
A file reading class that provides buffered and unbuffered access to files with support for non-POD c...
Definition: File.h:483
size_t Read(Data1D< T > &data, const size_t amnt_to_read)
Reads amnt_to_read elements of plain old data type T into data without buffering, resizing if too sma...
Definition: File.h:569
A Mersenne Twister random number generator class with integer, array, or time-based seeding.
Definition: RND.h:327
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:465
void Seed(u32 seed)
Reseed the random number generator with the value seed.
Definition: RND.h:334
RND()
Default constructor, which seeds using the system time and 64 bits of environmental entropy.
Definition: RND.h:410
RND(u32 seed)
Construct the generator with the initial seed given.
Definition: RND.h:390
RND(const Data1D< u32 > &init_key)
Construct the generator with the initial seed being the first 624 values of init_key.
Definition: RND.h:400
void Seed(const Data1D< u32 > &init_key)
Reseed the generator with up to the first 624 values of init_key.
Definition: RND.h:344
static const u32 CONST_SEED
The reference implementation's recommended seed.
Definition: RND.h:331
void Seed_urandom()
Seed the genederator by reading 624 u32's from /dev/urandom.
Definition: RND.h:430
static f64 Get()
Returns seconds since 1970-01-01 epoch to a precision between 100 nanoseconds and 1 microsecond.
Definition: RTime.h:111
static f64 GetPrecision()
Empirically determines the precision of Get(), and stores this value for future calls to this.
Definition: RTime.h:151
Cryptographically strong RNG, uses /dev/urandom.
Definition: RND.h:549
URand(const size_t buf_size=128)
Default constructor.
Definition: RND.h:555
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:563