File: rng.h

package info (click to toggle)
r-cran-spacefillr 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,472 kB
  • sloc: ansic: 32,867; cpp: 5,657; makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download
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
#ifndef RNGH
#define RNGH

#include <random>
#define extended extended_rng
#include "pcg/pcg_random.hpp"

class random_gen {
public:
  // Define result_type for compatibility with standard library requirements
  using result_type = uint32_t;

  random_gen(unsigned int seed) : rng(seed) {}
  random_gen() : rng(pcg_extras::seed_seq_from<std::random_device>{}) {}

  // The uniform random number generator function
  float unif_rand() {
    return std::ldexp(rng(), -32);
  }

  uint32_t UniformUInt32(uint32_t b) {
    uint32_t threshold = (~b + 1u) % b;
    while (true) {
      uint32_t r = rng();
      if (r >= threshold)
        return r % b;
    }
  }

  // Call operator to satisfy UniformRandomBitGenerator requirements
  result_type operator()() {
    return rng();
  }

  // Static methods to satisfy UniformRandomBitGenerator requirements
  static constexpr result_type min() {
    return pcg32::min();
  }

  static constexpr result_type max() {
    return pcg32::max();
  }

  pcg32 rng;
};

#endif