rcolyer.net
RC Lib  Version 202403231100
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  std::cout << "Delta arr: " << delta_arr << "\n";
307 
308  return entropy;
309  }
310 
311 
312  private:
313 
314  u32 u8_rndstore;
315  u32 u8_offset;
316  u32 u16_rndstore;
317  u32 u16_offset;
318  };
319 
320 
321 
322  /// A Mersenne Twister random number generator class with integer, array,
323  /// or time-based seeding.
324  /** The output of this class is compliant with the mt19937ar reference
325  * implementation by Nishimura & Matsumoto.
326  */
327  class RND : public BaseRND {
328  public:
329 
330  /// The reference implementation's recommended seed.
331  static const u32 CONST_SEED=5489;
332 
333  /// Reseed the random number generator with the value seed.
334  inline void Seed(u32 seed) {
335  MT[0] = seed;
336 
337  for (size_t i=1; i<MT.size(); i++) {
338  MT[i] = (1812433253 * (MT[i-1] ^ (MT[i-1] >> 30)) + i);
339  }
340  }
341 
342 
343  /// Reseed the generator with up to the first 624 values of init_key.
344  inline void Seed(const Data1D<u32>& init_key) {
345  Seed(19650218);
346 
347  if (init_key.size() == 0) {
348  return;
349  }
350 
351  int i, k, end;
352  size_t j;
353  i=1;
354  j=0;
355  end = (624 > init_key.size()) ? 624 : init_key.size();
356 
357  for (k=0; k<end; k++) {
358  MT[i] = (MT[i] ^ ((MT[i-1]) ^ (MT[i-1] >> 30)) * 1664525)
359  + init_key[j] + j;
360  MT[i] &= 0xFFFFFFFF;
361 
362  i++;
363  if (i >= 624) {
364  MT[0] = MT[623];
365  i=1;
366  }
367 
368  j++;
369  if (j >= init_key.size()) {
370  j=0;
371  }
372  }
373 
374  for (k=0; k<623; k++) {
375  MT[i] = (MT[i] ^ ((MT[i-1] ^ (MT[i-1] >> 30)) * 1566083941)) - i;
376  MT[i] &= 0xFFFFFFFF;
377 
378  i++;
379  if (i >= 624) {
380  MT[0] = MT[623];
381  i=1;
382  }
383  }
384 
385  MT[0] = 0x80000000;
386  }
387 
388 
389  /// Construct the generator with the initial seed given.
390  inline RND(u32 seed)
391  : MT (Data1D<u32>(624)),
392  u32_index(624) {
393 
394  Seed(seed);
395  }
396 
397 
398  /// Construct the generator with the initial seed being the first 624
399  /// values of init_key.
400  inline RND(const Data1D<u32>& init_key)
401  : MT (Data1D<u32>(624)),
402  u32_index(624) {
403 
404  Seed(init_key);
405  }
406 
407 
408  /// Default constructor, which seeds using the system time and 64
409  /// bits of environmental entropy.
410  inline RND()
411  : MT (Data1D<u32>(624)),
412  u32_index(624) {
413 
414  f64 time;
415  time = Time::Get();
416  u64 entropy = GetEntropy();
417 
418  Data1D<u32> time_seed(4);
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);
423 
424  Seed(time_seed);
425  }
426 
427 
428 #ifdef RC_HAVE_URAND
429  /// Seed the genederator by reading 624 u32's from /dev/urandom.
430  inline void Seed_urandom() {
431  FileRead urand(RC_DEV_URANDOM);
432  Data1D<u32> init_key(624);
433  urand.Read(init_key);
434  Seed(init_key);
435  }
436 #endif
437 
438 
439  private:
440 
441  inline void UpdateMT() {
442  int i;
443  u32 val;
444  u32 xorarr[2] = {0, 0x9908b0df};
445 
446  // Partially unrolled to remove modulos. Significant performance gain.
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];
451  }
452  for (; i<623; i++) {
453  val = (MT[i] & 0x80000000) + (MT[i+1] & 0x7FFFFFFF);
454  MT[i] = MT[i-227] ^ (val>>1);
455  MT[i] ^= xorarr[val & 1];
456  }
457  val = (MT[i] & 0x80000000) + (MT[0] & 0x7FFFFFFF);
458  MT[i] = MT[396] ^ (val>>1);
459  MT[i] ^= xorarr[val & 1];
460  }
461 
462  public:
463 
464  /// Provides random u32 values.
465  inline virtual u32 Get_u32() {
466  u32 val;
467 
468  if (u32_index >= MT.size()) {
469  UpdateMT();
470  u32_index = 0;
471  }
472 
473  val = MT[u32_index];
474 
475  val ^= (val >> 11);
476  val ^= ((val << 7) & 0x9d2c5680);
477  val ^= ((val << 15) & 0xefc60000);
478  val ^= (val >> 18);
479 
480  u32_index++;
481 
482  return val;
483  }
484 
485 
486  private:
487 
488  Data1D<u32> MT;
489  u16 u32_index;
490  };
491 
492 
493  /// Provides true random numbers sourced from environmental noise.
494  /** See the entropy source BaseRND::GetEntropy() for details on the method.
495  * These numbers should be cryptographically strong.
496  */
497  class EntropyRND : public BaseRND {
498  public:
499 
500  /// Default constructor.
501  EntropyRND() : u32_rndstore(0), u32_offset(64) { }
502 
503  /// Provides random u32 values.
504  inline virtual u32 Get_u32() {
505  u32 retval;
506 
507  if (u32_offset >= 64) {
508  u32_rndstore = GetEntropy();
509  u32_offset = 0;
510  }
511 
512  retval = u32(u32_rndstore >> u32_offset);
513  u32_offset += 32;
514 
515  return retval;
516  }
517 
518  private:
519 
520  u64 u32_rndstore;
521  u32 u32_offset;
522  };
523 
524  /// @cond UNDOC
525 #ifdef __MINGW32__
526 #define RC_MINGW_2014_BUGFIX
527 #else
528 #define RC_MINGW_2014_BUGFIX RC_THREAD_LOCAL
529 #endif
530  /// @endcond
531 
532 
533  /// \def RC_MAKE_GET_RANGE
534  /// Generates a family of RND_Get_Range functions.
535 #define RC_MAKE_GET_RANGE(classname) \
536  /** \brief Uses classname as a RandomNumberGenerator, e.g. for random_shuffle
537  @param range The range of values that can be returned, starting with 0.
538  @return A random value from 0 up to and including range-1.
539  */\
540  inline u64 classname##_Get_Range(u64 range) {\
541  static RC_MINGW_2014_BUGFIX classname rng;\
542  return rng.GetRange(range);\
543  }
544 
547 
548 #ifdef RC_HAVE_URAND
549  /// Cryptographically strong RNG, uses /dev/urandom
550  /** The interface is identical to RND.h except there are no seed functions.
551  */
552  class URand : public BaseRND {
553  public:
554 
555  /// Default constructor.
556  /** @param buf_size The number of u32 values to read at a time.
557  */
558  inline URand(const size_t buf_size=128)
559  : urand_buf (Data1D<u32>(buf_size)),
560  urand_fr (RC_DEV_URANDOM),
561  index (urand_buf.size()) {
562  }
563 
564 
565  /// Provides random u32 values.
566  inline virtual u32 Get_u32() {
567  if (index >= urand_buf.size()) {
568  urand_fr.Read(urand_buf);
569  index = 0;
570  }
571 
572  return urand_buf[index++];
573  }
574 
575  private:
576 
577  Data1D<u32> urand_buf;
578  FileRead urand_fr;
579  size_t index;
580  };
581 
582 
583  RC_MAKE_GET_RANGE(URand);
584 
585 #endif // RC_HAVE_URAND
586 
587 
588  /// \def RC_MAKE_RND_GEN
589  /// Generates a family of random generator singletons.
590  #define RC_MAKE_RND_GEN(classname) \
591  /** \brief This returns a singleton of classname. */\
592  inline classname& classname##_Gen() {\
593  static RC_MINGW_2014_BUGFIX classname rng;\
594  return rng;\
595  }
596 
599 #ifdef RC_HAVE_URAND
601 #endif
602 
603 
604  /// @cond UNDOC
605  class RC_BaseRND_Float_Bounds_Checker {
606  protected:
607  /// If this array gives a compile error, then Get_f32() can possibly
608  /// return a 1.0!
609  /** Either raise the value of the divisor to fix this, or comment out
610  * this check if you do not care.
611  */
612  char RC_Get_f32_Never_Return_One_Check[int((1-0xFFFFFFFF/RC_BASERND_u32_to_f32_divisor)*1e10)];
613 
614  /// If this array gives a compile error, then Get_f64() can possibly
615  /// return a 1.0!
616  /** Either raise the value of the divisor to fix this, or comment out
617  * this check if you do not care.
618  */
619  char RC_Get_f64_Never_Return_One_Check[int((1-0xFFFFFFFFFFFFFFFFull/RC_BASERND_u64_to_f64_divisor)*1e20)];
620  };
621  /// @endcond
622 }
623 
624 #endif // RC_RND_H
625 
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
Definition: APtr.h:25
email address
— (c) 2015