SpectMorph
smrandom.hh
1 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html
2 
3 #ifndef SPECTMORPH_RANDOM_HH
4 #define SPECTMORPH_RANDOM_HH
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 
9 #include "smpcg32rng.hh"
10 
11 namespace SpectMorph
12 {
13 
14 class Random
15 {
16  Pcg32Rng rand_gen;
17 public:
18  Random();
19 
20  void set_seed (uint32_t seed);
21 
22  inline double
23  random_double_range (double begin, double end)
24  {
25  const uint32_t rand_max = 0xffffffff; // Pcg32Rng output: complete 32-bit values
26  const uint32_t r = random_uint32();
27  const double scale = 1.0 / (double (rand_max) + 1.0);
28 
29  return r * scale * (end - begin) + begin;
30  }
31  inline uint32_t
32  random_uint32()
33  {
34  return rand_gen.random();
35  }
36  inline void
37  random_block (size_t n_values, uint32_t *values)
38  {
39  while (n_values--)
40  *values++ = random_uint32();
41  }
42 };
43 
44 }
45 #endif
uint32_t random()
Generate uniformly distributed 32 bit pseudo random number.
Definition: smpcg32rng.hh:83
Definition: smrandom.hh:14
Definition: smpcg32rng.hh:24
Definition: smalignedarray.cc:7