Debian Codegraph @

src/mpt/random/random.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_RANDOM_RANDOM_HPP
4 #define MPT_RANDOM_RANDOM_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/integer.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/random/engine.hpp"
12
13 #include <limits>
14 #include <type_traits>
15
16 #include <cstddef>
17
18
19 namespace mpt {
20 inline namespace MPT_INLINE_NS {
21
22
23
24 template <typename T, typename Trng>
25 inline T random(Trng & rng) {
26 static_assert(std::numeric_limits<T>::is_integer);
27 typedef typename std::make_unsigned<T>::type unsigned_T;
28 const unsigned int rng_bits = mpt::engine_traits<Trng>::result_bits();
29 unsigned_T result = 0;
30 for (std::size_t entropy = 0; entropy < (sizeof(T) * 8); entropy += rng_bits) {
31 if constexpr (rng_bits < (sizeof(T) * 8)) {
32 constexpr unsigned int shift_bits = rng_bits % (sizeof(T) * 8); // silence utterly stupid MSVC and GCC warnings about shifting by too big amount (in which case this branch is not even taken however)
33 result = (result << shift_bits) ^ static_cast<unsigned_T>(rng());
34 } else {
35 result = static_cast<unsigned_T>(rng());
36 }
37 }
38 return static_cast<T>(result);
39 }
40
41 template <typename T, std::size_t required_entropy_bits, typename Trng>
42 inline T random(Trng & rng) {
43 static_assert(std::numeric_limits<T>::is_integer);
44 typedef typename std::make_unsigned<T>::type unsigned_T;
45 const unsigned int rng_bits = mpt::engine_traits<Trng>::result_bits();
46 unsigned_T result = 0;
47 for (std::size_t entropy = 0; entropy < std::min(required_entropy_bits, sizeof(T) * 8); entropy += rng_bits) {
48 if constexpr (rng_bits < (sizeof(T) * 8)) {
49 constexpr unsigned int shift_bits = rng_bits % (sizeof(T) * 8); // silence utterly stupid MSVC and GCC warnings about shifting by too big amount (in which case this branch is not even taken however)
50 result = (result << shift_bits) ^ static_cast<unsigned_T>(rng());
51 } else {
52 result = static_cast<unsigned_T>(rng());
53 }
54 }
55 if constexpr (required_entropy_bits >= (sizeof(T) * 8)) {
56 return static_cast<T>(result);
57 } else {
58 return static_cast<T>(result & ((static_cast<unsigned_T>(1) << required_entropy_bits) - static_cast<unsigned_T>(1)));
59 }
60 }
61
62 template <typename T, typename Trng>
63 inline T random(Trng & rng, std::size_t required_entropy_bits) {
64 static_assert(std::numeric_limits<T>::is_integer);
65 typedef typename std::make_unsigned<T>::type unsigned_T;
66 const unsigned int rng_bits = mpt::engine_traits<Trng>::result_bits();
67 unsigned_T result = 0;
68 for (std::size_t entropy = 0; entropy < std::min(required_entropy_bits, sizeof(T) * 8); entropy += rng_bits) {
69 if constexpr (rng_bits < (sizeof(T) * 8)) {
70 constexpr unsigned int shift_bits = rng_bits % (sizeof(T) * 8); // silence utterly stupid MSVC and GCC warnings about shifting by too big amount (in which case this branch is not even taken however)
71 result = (result << shift_bits) ^ static_cast<unsigned_T>(rng());
72 } else {
73 result = static_cast<unsigned_T>(rng());
74 }
75 }
76 if (required_entropy_bits >= (sizeof(T) * 8)) {
77 return static_cast<T>(result);
78 } else {
79 return static_cast<T>(result & ((static_cast<unsigned_T>(1) << required_entropy_bits) - static_cast<unsigned_T>(1)));
80 }
81 }
82
83 template <typename T>
84 struct uniform_real_distribution {
85 private:
86 T a;
87 T b;
88
89 public:
90 inline uniform_real_distribution(T a_, T b_)
91 : a(a_)
92 , b(b_) {
93 return;
94 }
95 template <typename Trng>
96 inline T operator()(Trng & rng) const {
97 const int mantissa_bits = std::numeric_limits<T>::digits;
98 return ((b - a) * static_cast<T>(mpt::random<uint64, mantissa_bits>(rng)) / static_cast<T>((static_cast<uint64>(1u) << mantissa_bits))) + a;
99 }
100 };
101
102
103 template <typename T, typename Trng, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
104 inline T random(Trng & rng, T min, T max) {
105 #if MPT_COMPILER_MSVC
106 #pragma warning(push)
107 #pragma warning(disable : 4018) // '<': signed/unsigned mismatch
108 #endif // MPT_COMPILER_MSVC
109 static_assert(std::numeric_limits<T>::is_integer);
110 if constexpr (std::is_same<T, uint8>::value) {
111 using dis_type = std::uniform_int_distribution<unsigned int>;
112 dis_type dis(min, max);
113 return static_cast<T>(dis(rng));
114 } else if constexpr (std::is_same<T, int8>::value) {
115 using dis_type = std::uniform_int_distribution<int>;
116 dis_type dis(min, max);
117 return static_cast<T>(dis(rng));
118 } else {
119 using dis_type = std::uniform_int_distribution<T>;
120 dis_type dis(min, max);
121 return static_cast<T>(dis(rng));
122 }
123 #if MPT_COMPILER_MSVC
124 #pragma warning(pop)
125 #endif // MPT_COMPILER_MSVC
126 }
127
128
129 template <typename T, typename Trng, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
130 inline T random(Trng & rng, T min, T max) {
131 static_assert(!std::numeric_limits<T>::is_integer);
132 using dis_type = mpt::uniform_real_distribution<T>;
133 dis_type dis(min, max);
134 return static_cast<T>(dis(rng));
135 }
136
137
138
139 } // namespace MPT_INLINE_NS
140 } // namespace mpt
141
142
143
144 #endif // MPT_RANDOM_RANDOM_HPP