userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
rand.hpp
Go to the documentation of this file.
1
#pragma once
2
6
7
#include <algorithm>
8
#include <cstdint>
9
#include <limits>
10
#include <random>
11
#include <utility>
12
13
#include <
userver/compiler/thread_local.hpp
>
14
#include <
userver/utils/assert.hpp
>
15
16
USERVER_NAMESPACE_BEGIN
17
18
namespace
utils
{
19
22
class
RandomBase
{
23
public
:
24
using
result_type = uint32_t;
25
26
virtual
~RandomBase
() =
default
;
27
28
virtual
result_type operator()() = 0;
29
30
static
constexpr
result_type min() {
return
std::mt19937::min(); }
31
static
constexpr
result_type max() {
return
std::mt19937::max(); }
32
};
33
34
namespace
impl {
35
36
std::seed_seq MakeSeedSeq();
37
38
class
RandomImpl final :
public
RandomBase
{
39
public
:
40
RandomImpl();
41
42
result_type operator()()
override
{
return
gen_(); }
43
44
private
:
45
std::mt19937 gen_;
46
};
47
48
compiler::ThreadLocalScope<RandomImpl> UseLocalRandomImpl();
49
50
}
// namespace impl
51
76
template
<
typename
Func>
77
decltype
(
auto
)
WithDefaultRandom
(Func&& func) {
78
auto
random = impl::UseLocalRandomImpl();
79
return
std::forward<Func>(func)(
static_cast<
RandomBase
&
>
(*random));
80
}
81
85
template
<
typename
T>
86
T
RandRange
(T from_inclusive, T to_exclusive) {
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});
90
}
else
{
91
// 8-bit types are not allowed in uniform_int_distribution, so increase the
92
// T size.
93
return
utils::WithDefaultRandom
(std::uniform_int_distribution<std::common_type_t<T, unsigned short>>{
94
from_inclusive,
95
to_exclusive - 1
96
});
97
}
98
}
99
102
template
<
typename
T>
103
T
RandRange
(T to_exclusive) {
104
return
utils::RandRange
(T{0}, to_exclusive);
105
}
106
110
template
<
typename
Container>
111
void
Shuffle
(Container& container) {
112
utils::WithDefaultRandom
([&container](
RandomBase
& rng) { std::ranges::shuffle(container, rng); });
113
}
114
118
std::uint32_t
Rand
();
119
120
}
// namespace utils
121
122
USERVER_NAMESPACE_END
userver
utils
rand.hpp
Generated on
for userver by
Doxygen
1.17.0