GCC Code Coverage Report


Directory: ./
File: include/randhub.hpp
Date: 2026-12-03 12:28:13
Exec Total Coverage
Lines: 0 16 0.0%
Functions: 0 5 0.0%
Branches: 0 4 0.0%
Decisions: 0 0 -%

Line Branch Decision Exec Source
1 #ifndef RANDHUB_H
2 #define RANDHUB_H
3
4 #include <limits>
5 #include <chrono>
6 #include <numeric>
7 #include <random>
8
9 template <class Real = double, class Int = unsigned long>
10 class RandHub
11 {
12 // This class contains an RNG engine and some convenience attributes
13 // and methods for drawing random numbers from various distributions.
14 std::mt19937_64 mt;
15 std::uniform_real_distribution<Real> real_dist{ 0.0, 1.0 };
16 std::uniform_int_distribution<Int> int_dist{ std::numeric_limits<Int>::min(), std::numeric_limits<Int>::max() };
17 std::normal_distribution<Real> norm_dist{ 0.0, 1.0 };
18
19 public:
20
21 ✗ RandHub():
22 ✗ mt(std::random_device{}())
23 {
24 ✗ }
25
26 RandHub(unsigned s):
27 mt(s)
28 {
29 }
30
31 // Real number in [0,1].
32 //
33 // NOTE
34 // Technically, the standard says that `uniform_real_distribution` should
35 // return a number in the *half-open* interval [0, ubound).
36 // However, there seems to be a bug in most compiler implementations
37 // whereby on rare occasions it could also return the upper bound,
38 // thus inadvertently matching the desired behaviour for this method.
39 // However, we cannot rely on that in case it is 'fixed' later on.
40 ✗ Real rand(const Real lbound, const Real ubound)
41 {
42 // Set the parameters of the distribution.
43 ✗ real_dist.param(typename std::uniform_real_distribution<Real>::param_type(
44 lbound, std::nexttoward(ubound, std::numeric_limits<long double>::max())));
45
46 ✗ return std::min(real_dist(mt), ubound);
47 }
48
49 ✗ Real rand(const Real ubound = 1.0) { return rand(0.0, ubound); }
50
51 // Real number in [0,ubound).
52 ✗ Real rand_exc(const Real lbound, const Real ubound)
53 {
54 ✗ return std::min(rand(lbound, ubound), std::nexttoward(ubound, static_cast<Real>(0.0)));
55 }
56
57 ✗ Real rand_exc(const Real ubound = 1.0) { return rand_exc(0.0, ubound); }
58
59 // Real number in (0,ubound).
60 ✗ Real rand_dbl_exc(const Real lbound, const Real ubound)
61 {
62 ✗ return std::max(std::nexttoward(static_cast<Real>(0.0), 1.0), rand_exc(lbound, ubound));
63 }
64
65 ✗ Real rand_dbl_exc(const Real ubound = 1.0) { return rand_dbl_exc(0.0, ubound); }
66
67 // Integer in [0,ubound] for ubound < int_t::max.
68 ✗ Int rand_int(const Int lbound, const Int ubound)
69 {
70 // Set the parameters of the distribution.
71 int_dist.param(typename std::uniform_int_distribution<Int>::param_type(lbound, ubound));
72 ✗ return int_dist(mt);
73 }
74
75 ✗ Int rand_int(const Int ubound = std::numeric_limits<Int>::max()) { return rand_int(0, ubound); }
76
77 // Normal distribution.
78 Real rand_norm(const Real mean = 0.0, const Real stddev = 1.0)
79 {
80 // Set the parameters of the distribution.
81 norm_dist.param(typename std::normal_distribution<Real>::param_type(mean, stddev));
82 return norm_dist(mt);
83 }
84
85 // Same as rand().
86 Real operator()() { return rand(); }
87
88 // Seed the RNG engine.
89 void seed(const unsigned s) { mt.seed(s); }
90 };
91
92 #endif // RANDHUB_H
93