6#ifndef RANDOM_GEN_VALUE_H
7#define RANDOM_GEN_VALUE_H
22template <
typename RandType>
28 static_assert(std::is_arithmetic_v<RandType>,
"RandType must be arithmetic");
42 static RandType
random(
const RandType min,
const RandType max) {
43 if constexpr (std::is_integral_v<RandType>) {
45 static std::random_device rd;
46 static std::mt19937 gen(rd());
47 std::uniform_int_distribution<RandType> dis(min, max);
49 }
else if constexpr (std::is_floating_point_v<RandType>) {
51 static std::random_device rd;
52 static std::mt19937 gen(rd());
53 std::uniform_real_distribution<RandType> dis(min, max);
57 throw std::logic_error(
"Unsupported type for RandomGenerator");
74 template <
int MAX_TRY = 100>
75 static RandType
random_except(
const RandType min,
const RandType max,
const RandType except) {
76 if (min == max && min == except) {
77 throw std::logic_error(
"Cannot generate value: only one possible value which is excluded.");
80 for (
int i = 0; i < MAX_TRY; ++i) {
81 RandType result =
random(min, max);
86 throw std::runtime_error(
"Too many attempts to generate a value different from 'except'");
A templated class for generating random numbers of a specified arithmetic type.
Definition random_gen_value.h:23
static RandType random_except(const RandType min, const RandType max, const RandType except)
Generates a random number within a specified range, excluding a particular value.
Definition random_gen_value.h:75
static RandType random(const RandType min, const RandType max)
Compile-time assertion to ensure RandType is an arithmetic type. If RandType is not arithmetic (e....
Definition random_gen_value.h:42