rcolyer.net
RC Lib  Version 202503311402
RND.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2015, Ryan A. Colyer
4 // Distributed under the Boost Software License, v1.0. (LICENSE.txt)
5 //
6 /// \file RND.h
7 /// Provides random number generator classes.
8 /////////////////////////////////////////////////////////////////////
9 
10 #ifndef RC_RND_H
11 #define RC_RND_H
12 
13 #include "Macros.h"
14 #include "Types.h"
15 #include "RTime.h"
16 #include "Data1D.h"
17 #ifdef RC_HAVE_URAND
18 #include "RStr.h"
19 #include "File.h"
20 #endif
21 
22 #include <stdio.h>
23 
24 namespace RC {
25 
26  /// An abstract class which provides functions for obtaining randomness
27  /// in convenient forms.
28  /** It fulfills the UniformRandomNumberGenerator concept.
29  * @see RND
30  * @see EntropyRND
31  * @see URand
32  */
33  class BaseRND {
34  /// @cond PROTECTED
35  #define RC_BASERND_u32_to_f32_divisor 4294967425.0
36  #define RC_BASERND_u64_to_f64_divisor 18446744073709552645.0L
37  /// @endcond
38 
39  public:
40 
41  /// Default constructor.
43  : u8_offset(32),
44  u16_offset(32) {
45  }
46 
47  /// Implement this for all subclasses. All other functions get their
48  /// randomness from this.
49  /** @return A random unsigned 32 bit integer.
50  */
51  virtual u32 Get_u32() = 0;
52 
53 
54  /// Equivalent to Get_u32().
55  inline u32 operator()() { return Get_u32(); }
56 
57  /// For UniformRandomNumberGenerator compliance.
58  typedef u32 result_type;
59 
60  /// Returns 0 for UniformRandomNumberGenerator compliance.
61  constexpr static inline u32 min() { return 0; }
62 
63  /// Returns u32 max for UniformRandomNumberGenerator compliance.
64  constexpr static inline u32 max() { return 0xFFFFFFFF; }
65 
66 
67  /// Provides random i32 values.
68  inline i32 Get_i32() {
69  return i32(Get_u32());
70  }
71 
72 
73  /// Provides random u64 values.
74  inline u64 Get_u64() {
75  return ( (u64(Get_u32()) << 32) + Get_u32() );
76  }
77 
78 
79  /// Provides random i64 values.
80  inline i64 Get_i64() {
81  return i64(Get_u64());
82  }
83 
84 
85  /// Provides a random f32 in the range [0,1).
86  inline f32 Get_f32() {
87  return ( f32(Get_u32()) / RC_BASERND_u32_to_f32_divisor );
88  }
89 
90 
91  /// Provides a random f64 in the range [0,1).
92  inline f64 Get_f64() {
93  return ( f64(Get_u64()) / RC_BASERND_u64_to_f64_divisor );
94  }
95 
96 
97  /// Provides random u8 values.
98  inline u8 Get_u8() {
99  u8 retval;
100 
101  if (u8_offset >= 32) {
102  u8_rndstore = Get_u32();
103  u8_offset = 0;
104  }
105 
106  retval = u8(u8_rndstore >> u8_offset);
107  u8_offset += 8;
108 
109  return retval;
110  }
111 
112  /// Provides random i8 values.
113  inline i8 Get_i8() {
114  return i8(Get_u8());
115  }
116 
117  /// Provides random char values.
118  inline char Get_char() {
119  return char(Get_u8());
120  }
121 
122  /// Provides random u16 values.
123  inline u16 Get_u16() {
124  u16 retval;
125 
126  if (u16_offset >= 32) {
127  u16_rndstore = Get_u32();
128  u16_offset = 0;
129  }
130 
131  retval = u16(u16_rndstore >> u16_offset);
132  u16_offset += 16;
133 
134  return retval;
135  }
136 
137  /// Provides random i16 values.
138  inline i16 Get_i16() {
139  return i16(Get_u16());
140  }
141 
142 
145  RC_GetT(char);
154 
155 
156  /// Returns true with probability, which should be in the range [0,1].
157  /** @param probability The chance of returning true.
158  * @return Randomly true according to probability.
159  */
160  inline bool GetProb(f64 probability) {
161  return (Get_f64() < probability);
162  }
163 
164  /// Returns true with probability, which should be in the range [0,1].
165  /** @param probability The chance of returning true.
166  * @return Randomly true according to probability.
167  */
168  inline bool GetProb(f32 probability) {
169  return (Get_f32() < probability);
170  }
171 
172 
173  /// Returns [0,range)
174  /** Note: The precision comes from f64, so it is slightly less than
175  * full u64 precision.
176  * @param range The number of integers in the range.
177  * @return A value from 0 up to range-1.
178  */
179  inline u64 GetRange(u64 range) {
180  return range*Get_f64();
181  }
182  /// Like GetRange() but for i64.
183  inline i64 GetRange(i64 range) { return i64(GetRange(u64(range))); }
184 
185  /// Like GetRange() but for i32.
186  /** Note: The precision comes from f32, so it is slightly less than
187  * full u32 precision.
188  */
189  inline u32 GetRange(u32 range) {
190  return range*Get_f32();
191  }
192  /// Like GetRange() but for i32.
193  inline i32 GetRange(i32 range) { return i32(GetRange(u32(range))); }
194 
195  /// Provides a random integer value in the range [low,high].
196  template <class T>
197  inline T GetRange(T low, T high) { return GetRange(high-low+1)+low;}
198 
199  /// Provides a random float in the range [low,high).
200  inline f64 GetFRange(f64 low, f64 high) {
201  return Get_f64()*(high-low)+low;
202  }
203 
204 
205  /// For any supported data type, fills the Data1D with random values.
206  template <class T>
207  inline void Fill(Data1D<T>& data) {
208  size_t i;
209 
210  for (i=0; i<data.size(); i++) {
211  Get(data[i]);
212  }
213  }
214 
215  /// Returns a random element from the Data1D.
216  template <class T>
217  inline T GetFrom(Data1D<T>& data) {
218  return data[GetRange(data.size())];
219  }
220 
221  private:
222  template<class T> inline static T EntAbs(T x) {
223  return (((x)<0)?(-(x)):(x));
224  }
225  inline static void EntRot(u64 &x, u32 left_by) {
226  x = (x << left_by) + (x >> (64-left_by));
227  }
228  public:
229 
230  /// Provides 64 bits of environmental entropy.
231  /** This function uses the jittering of a tight loop relative to the
232  * ticking of the high precision clock to generate entropy, which is then
233  * mixed with the Rijndael S-box. It automatically adapts to the system's
234  * clock precision and relative cpu speed.
235  * The randomness has passed dieharder version 3.31.1.
236  * @return 64 bits of environmental entropy.
237  */
238  inline static u64 GetEntropy() {
239  static RC_THREAD_LOCAL u64 entropy = 0;
240  u8 sbox_raw[256] = { // Rijndael S-box
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
263  };
264  Data1D<u8> sbox(256, sbox_raw);
265 
266  static RC_THREAD_LOCAL f64 half_prec = 0.5*Time::GetPrecision();
267  static RC_THREAD_LOCAL f64 delta = half_prec;
268  const u32 empirical_entropy_factor = 32; // ~20
269  u64 loopdiffsum = 0;
270  u64 last_loops = 0;
271  Data1D<f64> delta_arr;
272 
273  for (u32 i=0; i < 64*empirical_entropy_factor; i++) {
274  u64 loops = 0;
275 
276  f64 start_t = RC::Time::Get();
277  while (EntAbs(RC::Time::Get() - start_t) <= delta) { loops++; }
278  delta /= 0.98 + 0.04*(loops>0); // Adapt time to 50/50 LSB flip
279  delta_arr += delta;
280  if (delta < half_prec) { delta = half_prec; }
281 
282  if (i > 0) {
283  i64 loopdiff = (i64)loops - (i64)last_loops;
284  if (loopdiff < 0) { loopdiff = -loopdiff; }
285  loopdiffsum += loopdiff;
286  }
287  last_loops = loops;
288 
289  entropy ^= loops;
290  EntRot(entropy, 8);
291 
292  if ((i&7) == 7) {
293  for (u32 b=0; b<8; b++) {
294  u32 shift = b*8;
295  u64 keep_mask = ~(u64(0xFFL) << shift);
296  u8 look_up = u8(entropy >> shift);
297  entropy = (entropy & keep_mask) + (u64(sbox[look_up]) << shift);
298  }
299  EntRot(entropy, 3);
300  }
301 
302  if ((i&7) == 0 && loopdiffsum > 64*empirical_entropy_factor) {
303  break; // High entropy estimate / slow jittery loop detected.
304  }
305  }
306 
307  return entropy;
308  }
309 
310 
311  private:
312 
313  u32 u8_rndstore;
314  u32 u8_offset;
315  u32 u16_rndstore;
316  u32 u16_offset;
317  };
318 
319 
320 
321  /// A Mersenne Twister random number generator class with integer, array,
322  /// or time-based seeding.
323  /** The output of this class is compliant with the mt19937ar reference
324  * implementation by Nishimura & Matsumoto.
325  */
326  class RND : public BaseRND {
327  public:
328 
329  /// The reference implementation's recommended seed.
330  static const u32 CONST_SEED=5489;
331 
332  /// Reseed the random number generator with the value seed.
333  inline void Seed(u32 seed) {
334  MT[0] = seed;
335 
336  for (size_t i=1; i<MT.size(); i++) {
337  MT[i] = (1812433253 * (MT[i-1] ^ (MT[i-1] >> 30)) + i);
338  }
339  }
340 
341 
342  /// Reseed the generator with up to the first 624 values of init_key.
343  inline void Seed(const Data1D<u32>& init_key) {
344  Seed(19650218);
345 
346  if (init_key.size() == 0) {
347  return;
348  }
349 
350  int i, k, end;
351  size_t j;
352  i=1;
353  j=0;
354  end = (624 > init_key.size()) ? 624 : init_key.size();
355 
356  for (k=0; k<end; k++) {
357  MT[i] = (MT[i] ^ ((MT[i-1]) ^ (MT[i-1] >> 30)) * 1664525)
358  + init_key[j] + j;
359  MT[i] &= 0xFFFFFFFF;
360 
361  i++;
362  if (i >= 624) {
363  MT[0] = MT[623];
364  i=1;
365  }
366 
367  j++;
368  if (j >= init_key.size()) {
369  j=0;
370  }
371  }
372 
373  for (k=0; k<623; k++) {
374  MT[i] = (MT[i] ^ ((MT[i-1] ^ (MT[i-1] >> 30)) * 1566083941)) - i;
375  MT[i] &= 0xFFFFFFFF;
376 
377  i++;
378  if (i >= 624) {
379  MT[0] = MT[623];
380  i=1;
381  }
382  }
383 
384  MT[0] = 0x80000000;
385  }
386 
387 
388  /// Construct the generator with the initial seed given.
389  inline RND(u32 seed)
390  : MT (Data1D<u32>(624)),
391  u32_index(624) {
392 
393  Seed(seed);
394  }
395 
396 
397  /// Construct the generator with the initial seed being the first 624
398  /// values of init_key.
399  inline RND(const Data1D<u32>& init_key)
400  : MT (Data1D<u32>(624)),
401  u32_index(624) {
402 
403  Seed(init_key);
404  }
405 
406 
407  /// Default constructor, which seeds using the system time and 64
408  /// bits of environmental entropy.
409  inline RND()
410  : MT (Data1D<u32>(624)),
411  u32_index(624) {
412 
413  f64 time;
414  time = Time::Get();
415  u64 entropy = GetEntropy();
416 
417  Data1D<u32> time_seed(4);
418  time_seed[0] = u32(time*1e9);
419  time_seed[1] = u32(time);
420  time_seed[2] = u32(entropy);
421  time_seed[3] = u32(entropy>>32);
422 
423  Seed(time_seed);
424  }
425 
426 
427 #ifdef RC_HAVE_URAND
428  /// Seed the genederator by reading 624 u32's from /dev/urandom.
429  inline void Seed_urandom() {
430  FileRead urand(RC_DEV_URANDOM);
431  Data1D<u32> init_key(624);
432  urand.Read(init_key);
433  Seed(init_key);
434  }
435 #endif
436 
437 
438  private:
439 
440  inline void UpdateMT() {
441  int i;
442  u32 val;
443  u32 xorarr[2] = {0, 0x9908b0df};
444 
445  // Partially unrolled to remove modulos. Significant performance gain.
446  for (i=0; i<227; i++) {
447  val = (MT[i] & 0x80000000) + (MT[i+1] & 0x7FFFFFFF);
448  MT[i] = MT[i+397] ^ (val>>1);
449  MT[i] ^= xorarr[val & 1];
450  }
451  for (; i<623; i++) {
452  val = (MT[i] & 0x80000000) + (MT[i+1] & 0x7FFFFFFF);
453  MT[i] = MT[i-227] ^ (val>>1);
454  MT[i] ^= xorarr[val & 1];
455  }
456  val = (MT[i] & 0x80000000) + (MT[0] & 0x7FFFFFFF);
457  MT[i] = MT[396] ^ (val>>1);
458  MT[i] ^= xorarr[val & 1];
459  }
460 
461  public:
462 
463  /// Provides random u32 values.
464  inline virtual u32 Get_u32() {
465  u32 val;
466 
467  if (u32_index >= MT.size()) {
468  UpdateMT();
469  u32_index = 0;
470  }
471 
472  val = MT[u32_index];
473 
474  val ^= (val >> 11);
475  val ^= ((val << 7) & 0x9d2c5680);
476  val ^= ((val << 15) & 0xefc60000);
477  val ^= (val >> 18);
478 
479  u32_index++;
480 
481  return val;
482  }
483 
484 
485  private:
486 
487  Data1D<u32> MT;
488  u16 u32_index;
489  };
490 
491 
492  /// Provides true random numbers sourced from environmental noise.
493  /** See the entropy source BaseRND::GetEntropy() for details on the method.
494  * These numbers should be cryptographically strong.
495  */
496  class EntropyRND : public BaseRND {
497  public:
498 
499  /// Default constructor.
500  EntropyRND() : u32_rndstore(0), u32_offset(64) { }
501 
502  /// Provides random u32 values.
503  inline virtual u32 Get_u32() {
504  u32 retval;
505 
506  if (u32_offset >= 64) {
507  u32_rndstore = GetEntropy();
508  u32_offset = 0;
509  }
510 
511  retval = u32(u32_rndstore >> u32_offset);
512  u32_offset += 32;
513 
514  return retval;
515  }
516 
517  private:
518 
519  u64 u32_rndstore;
520  u32 u32_offset;
521  };
522 
523  /// @cond UNDOC
524 #ifdef __MINGW32__
525 #define RC_MINGW_2014_BUGFIX
526 #else
527 #define RC_MINGW_2014_BUGFIX RC_THREAD_LOCAL
528 #endif
529  /// @endcond
530 
531 
532  /// \def RC_MAKE_GET_RANGE
533  /// Generates a family of RND_Get_Range functions.
534 #define RC_MAKE_GET_RANGE(classname) \
535  /** \brief Uses classname as a RandomNumberGenerator, e.g. for random_shuffle
536  @param range The range of values that can be returned, starting with 0.
537  @return A random value from 0 up to and including range-1.
538  */\
539  inline u64 classname##_Get_Range(u64 range) {\
540  static RC_MINGW_2014_BUGFIX classname rng;\
541  return rng.GetRange(range);\
542  }
543 
546 
547 #ifdef RC_HAVE_URAND
548  /// Cryptographically strong RNG, uses /dev/urandom
549  /** The interface is identical to RND.h except there are no seed functions.
550  */
551  class URand : public BaseRND {
552  public:
553 
554  /// Default constructor.
555  /** @param buf_size The number of u32 values to read at a time.
556  */
557  inline URand(const size_t buf_size=128)
558  : urand_buf (Data1D<u32>(buf_size)),
559  urand_fr (RC_DEV_URANDOM),
560  index (urand_buf.size()) {
561  }
562 
563 
564  /// Provides random u32 values.
565  inline virtual u32 Get_u32() {
566  if (index >= urand_buf.size()) {
567  urand_fr.Read(urand_buf);
568  index = 0;
569  }
570 
571  return urand_buf[index++];
572  }
573 
574  private:
575 
576  Data1D<u32> urand_buf;
577  FileRead urand_fr;
578  size_t index;
579  };
580 
581 
582  RC_MAKE_GET_RANGE(URand);
583 
584 #endif // RC_HAVE_URAND
585 
586 
587  /// \def RC_MAKE_RND_GEN
588  /// Generates a family of random generator singletons.
589  #define RC_MAKE_RND_GEN(classname) \
590  /** \brief This returns a singleton of classname. */\
591  inline classname& classname##_Gen() {\
592  static RC_MINGW_2014_BUGFIX classname rng;\
593  return rng;\
594  }
595 
598 #ifdef RC_HAVE_URAND
600 #endif
601 
602 
603  /// @cond UNDOC
604  class RC_BaseRND_Float_Bounds_Checker {
605  protected:
606  /// If this array gives a compile error, then Get_f32() can possibly
607  /// return a 1.0!
608  /** Either raise the value of the divisor to fix this, or comment out
609  * this check if you do not care.
610  */
611  char RC_Get_f32_Never_Return_One_Check[int((1-0xFFFFFFFF/RC_BASERND_u32_to_f32_divisor)*1e10)];
612 
613  /// If this array gives a compile error, then Get_f64() can possibly
614  /// return a 1.0!
615  /** Either raise the value of the divisor to fix this, or comment out
616  * this check if you do not care.
617  */
618  char RC_Get_f64_Never_Return_One_Check[int((1-0xFFFFFFFFFFFFFFFFull/RC_BASERND_u64_to_f64_divisor)*1e20)];
619  };
620  /// @endcond
621 }
622 
623 #endif // RC_RND_H
624 
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:586
#define RC_MAKE_GET_RANGE(classname)
Generates a family of RND_Get_Range functions.
Definition: RND.h:534
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:496
EntropyRND()
Default constructor.
Definition: RND.h:500
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:503
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:326
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:464
void Seed(u32 seed)
Reseed the random number generator with the value seed.
Definition: RND.h:333
RND()
Default constructor, which seeds using the system time and 64 bits of environmental entropy.
Definition: RND.h:409
RND(u32 seed)
Construct the generator with the initial seed given.
Definition: RND.h:389
RND(const Data1D< u32 > &init_key)
Construct the generator with the initial seed being the first 624 values of init_key.
Definition: RND.h:399
void Seed(const Data1D< u32 > &init_key)
Reseed the generator with up to the first 624 values of init_key.
Definition: RND.h:343
static const u32 CONST_SEED
The reference implementation's recommended seed.
Definition: RND.h:330
void Seed_urandom()
Seed the genederator by reading 624 u32's from /dev/urandom.
Definition: RND.h:429
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:548
URand(const size_t buf_size=128)
Default constructor.
Definition: RND.h:554
virtual u32 Get_u32()
Provides random u32 values.
Definition: RND.h:562
Definition: APtr.h:25
email address
— (c) 2015