13#include <userver/compiler/thread_local.hpp>
14#include <userver/utils/assert.hpp>
16USERVER_NAMESPACE_BEGIN
24 using result_type = uint32_t;
26 virtual ~RandomBase() =
default;
28 virtual result_type operator()() = 0;
30 static constexpr result_type min() {
return std::mt19937::min(); }
31 static constexpr result_type max() {
return std::mt19937::max(); }
36std::seed_seq MakeSeedSeq();
42 result_type operator()()
override {
return gen_(); }
48compiler::ThreadLocalScope<RandomImpl> UseLocalRandomImpl();
76template <
typename Func>
78 auto random = impl::UseLocalRandomImpl();
79 return std::forward<Func>(func)(
static_cast<RandomBase&>(*random));
87 UINVARIANT(from_inclusive < to_exclusive,
"attempt to get a random value in an incorrect range");
88 if constexpr (std::is_floating_point_v<T>) {
89 return utils::WithDefaultRandom(std::uniform_real_distribution<T>{from_inclusive, to_exclusive});
93 return utils::WithDefaultRandom(std::uniform_int_distribution<std::common_type_t<T,
unsigned short>>{
94 from_inclusive, to_exclusive - 1});
102 return RandRange(T{0}, to_exclusive);
108template <
typename Container>
111 std::shuffle(std::begin(container), std::end(container), rng);