forked from ceph/ceph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.h
288 lines (233 loc) · 7.9 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2017 SUSE LINUX GmbH
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#ifndef CEPH_RANDOM_H
#define CEPH_RANDOM_H 1
#include <mutex>
#include <random>
#include "boost/optional.hpp"
#include "common/backport14.h"
// Basic random number facility, adapted from N3551:
namespace ceph {
namespace util {
inline namespace version_1_0 {
namespace detail {
template <typename EngineT>
EngineT& engine();
template <typename MutexT, typename EngineT>
void randomize_rng(const int seed, MutexT& m, EngineT& e)
{
std::lock_guard<MutexT> lg(m);
e.seed(seed);
}
template <typename MutexT, typename EngineT>
void randomize_rng(MutexT& m, EngineT& e)
{
thread_local std::random_device rd;
std::lock_guard<MutexT> lg(m);
e.seed(rd());
}
template <typename EngineT = std::default_random_engine>
void randomize_rng(const int n)
{
detail::engine<EngineT>().seed(n);
}
template <typename EngineT = std::default_random_engine>
void randomize_rng()
{
thread_local std::random_device rd;
detail::engine<EngineT>().seed(rd());
}
template <typename EngineT>
EngineT& engine()
{
thread_local boost::optional<EngineT> rng_engine;
if (!rng_engine) {
rng_engine.emplace(EngineT());
randomize_rng<EngineT>();
}
return *rng_engine;
}
} // namespace detail
namespace detail {
template <typename NumberT,
typename DistributionT,
typename EngineT>
NumberT generate_random_number(const NumberT min, const NumberT max,
EngineT& e)
{
thread_local DistributionT d { min, max };
using param_type = typename DistributionT::param_type;
return d(e, param_type { min, max });
}
template <typename NumberT,
typename MutexT,
typename DistributionT,
typename EngineT>
NumberT generate_random_number(const NumberT min, const NumberT max,
MutexT& m, EngineT& e)
{
thread_local DistributionT d { min, max };
using param_type = typename DistributionT::param_type;
std::lock_guard<MutexT> lg(m);
return d(e, param_type { min, max });
}
template <typename NumberT,
typename DistributionT,
typename EngineT>
NumberT generate_random_number(const NumberT min, const NumberT max)
{
return detail::generate_random_number<NumberT, DistributionT, EngineT>
(min, max, detail::engine<EngineT>());
}
template <typename MutexT, typename EngineT,
int min = 0,
int max = std::numeric_limits<int>::max(),
typename DistributionT = std::uniform_int_distribution<int>>
int generate_random_number(MutexT& m, EngineT& e)
{
return detail::generate_random_number<int, MutexT, DistributionT, EngineT>
(min, max, m, e);
}
} // namespace detail
template <typename EngineT = std::default_random_engine>
void randomize_rng()
{
detail::randomize_rng<EngineT>();
}
template <typename IntegerT = int,
typename DistributionT = std::uniform_int_distribution<IntegerT>,
typename EngineT = std::default_random_engine>
IntegerT generate_random_number()
{
using limits = std::numeric_limits<IntegerT>;
return detail::generate_random_number<IntegerT, DistributionT, EngineT>
(limits::min(), limits::max());
}
template <typename IntegerT>
IntegerT generate_random_number(const IntegerT min, const IntegerT max,
ceph::enable_if_t<std::is_integral<IntegerT>::value>* = nullptr)
{
return detail::generate_random_number<IntegerT,
std::uniform_int_distribution<IntegerT>,
std::default_random_engine>
(min, max);
}
namespace detail {
template <typename IntegerT, typename MutexT, typename EngineT>
int generate_random_number(const IntegerT min, const IntegerT max,
MutexT& m, EngineT& e,
ceph::enable_if_t<std::is_integral<IntegerT>::value>* = nullptr)
{
return detail::generate_random_number<IntegerT, MutexT,
std::uniform_int_distribution<IntegerT>,
EngineT>
(min, max, m, e);
}
template <typename IntegerT, typename MutexT, typename EngineT>
int generate_random_number(const IntegerT max,
MutexT& m, EngineT& e,
ceph::enable_if_t<std::is_integral<IntegerT>::value>* = nullptr)
{
constexpr IntegerT zero = 0;
return generate_random_number(zero, max, m, e);
}
} // namespace detail
template <typename IntegerT>
int generate_random_number(const IntegerT max,
ceph::enable_if_t<std::is_integral<IntegerT>::value>* = nullptr)
{
constexpr IntegerT zero = 0;
return generate_random_number(zero, max);
}
template <typename RealT>
RealT generate_random_number(const RealT min, const RealT max,
ceph::enable_if_t<std::is_floating_point<RealT>::value>* = nullptr)
{
return detail::generate_random_number<RealT,
std::uniform_real_distribution<RealT>,
std::default_random_engine>
(min, max);
}
namespace detail {
template <typename RealT, typename MutexT>
RealT generate_random_number(const RealT max, MutexT& m,
ceph::enable_if_t<std::is_floating_point<RealT>::value>* = nullptr)
{
constexpr RealT zero = 0.0;
return generate_random_number(zero, max, m);
}
template <typename RealT, typename MutexT, typename EngineT>
RealT generate_random_number(const RealT min, const RealT max, MutexT& m, EngineT& e,
ceph::enable_if_t<std::is_floating_point<RealT>::value>* = nullptr)
{
return detail::generate_random_number<RealT, MutexT,
std::uniform_real_distribution<RealT>,
EngineT>
(min, max, m, e);
}
template <typename RealT, typename MutexT, typename EngineT>
RealT generate_random_number(const RealT max, MutexT& m, EngineT& e,
ceph::enable_if_t<std::is_floating_point<RealT>::value>* = nullptr)
{
constexpr RealT zero = 0.0;
return generate_random_number(zero, max, m, e);
}
} // namespace detail
template <typename RealT>
RealT generate_random_number(const RealT max,
ceph::enable_if_t<std::is_floating_point<RealT>::value>* = nullptr)
{
constexpr RealT zero = 0.0;
return generate_random_number(zero, max);
}
// Function object:
template <typename NumberT>
class random_number_generator final
{
std::mutex l;
std::random_device rd;
std::default_random_engine e;
public:
using number_type = NumberT;
public:
random_number_generator() {
detail::randomize_rng(l, e);
}
explicit random_number_generator(const int seed) {
detail::randomize_rng(seed, l, e);
}
random_number_generator(random_number_generator&& rhs)
: e(std::move(rhs.e))
{}
public:
random_number_generator(const random_number_generator&) = delete;
random_number_generator& operator=(const random_number_generator&) = delete;
public:
NumberT operator()() {
return detail::generate_random_number(l, e);
}
NumberT operator()(const NumberT max) {
return detail::generate_random_number(max, l, e);
}
NumberT operator()(const NumberT min, const NumberT max) {
return detail::generate_random_number(min, max, l, e);
}
public:
void seed(const int n) {
detail::randomize_rng(n, l, e);
}
};
} // inline namespace version_1_0
}} // namespace ceph::util
#endif