tests/rand_int: Expose PRNG

This commit is contained in:
Merry 2022-07-19 20:47:26 +01:00 committed by merry
parent 8e6467bf45
commit ecacb6cdc6

View file

@ -8,14 +8,20 @@
#include <random> #include <random>
#include <type_traits> #include <type_traits>
namespace detail {
inline std::mt19937 g_rand_int_generator = [] {
std::random_device rd;
std::mt19937 mt{rd()};
return mt;
}();
} // namespace detail
template<typename T> template<typename T>
T RandInt(T min, T max) { T RandInt(T min, T max) {
static_assert(std::is_integral_v<T>, "T must be an integral type."); static_assert(std::is_integral_v<T>, "T must be an integral type.");
static_assert(!std::is_same_v<T, signed char> && !std::is_same_v<T, unsigned char>, static_assert(!std::is_same_v<T, signed char> && !std::is_same_v<T, unsigned char>,
"Using char with uniform_int_distribution is undefined behavior."); "Using char with uniform_int_distribution is undefined behavior.");
static std::random_device rd;
static std::mt19937 mt(rd());
std::uniform_int_distribution<T> rand(min, max); std::uniform_int_distribution<T> rand(min, max);
return rand(mt); return rand(detail::g_rand_int_generator);
} }