proxygen
Random.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #define FOLLY_RANDOM_H_
19 
20 #include <array>
21 #include <cstdint>
22 #include <random>
23 #include <type_traits>
24 
25 #include <folly/Portability.h>
26 #include <folly/Traits.h>
28 
29 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
30 #include <ext/random>
31 #endif
32 
33 namespace folly {
34 
53  public:
55 
57 
58  static constexpr result_type min() {
60  }
61  static constexpr result_type max() {
63  }
64 };
65 
66 class Random {
67  private:
68  template <class RNG>
69  using ValidRNG = typename std::
70  enable_if<std::is_unsigned<invoke_result_t<RNG&>>::value, RNG>::type;
71 
72  template <class T>
73  class SecureRNG {
74  public:
75  using result_type = typename std::enable_if<
78 
80  return Random::secureRandom<result_type>();
81  }
82 
83  static constexpr result_type min() {
85  }
86 
87  static constexpr result_type max() {
89  }
90  };
91 
92  public:
93  // Default generator type.
94 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
95  typedef __gnu_cxx::sfmt19937 DefaultGenerator;
96 #else
97  typedef std::mt19937 DefaultGenerator;
98 #endif
99 
103  static void secureRandom(void* data, size_t len);
104 
108  template <class T>
109  static typename std::enable_if<
111  T>::type
113  T val;
114  secureRandom(&val, sizeof(val));
115  return val;
116  }
117 
122  return secureRandom<uint32_t>();
123  }
124 
129  SecureRNG<uint32_t> srng;
130  return rand32(max, srng);
131  }
132 
137  SecureRNG<uint32_t> srng;
138  return rand32(min, max, srng);
139  }
140 
145  return secureRandom<uint64_t>();
146  }
147 
152  SecureRNG<uint64_t> srng;
153  return rand64(max, srng);
154  }
155 
160  SecureRNG<uint64_t> srng;
161  return rand64(min, max, srng);
162  }
163 
167  static bool secureOneIn(uint32_t n) {
168  SecureRNG<uint32_t> srng;
169  return rand32(0, n, srng) == 0;
170  }
171 
175  static double secureRandDouble01() {
176  SecureRNG<uint64_t> srng;
177  return randDouble01(srng);
178  }
179 
183  static double secureRandDouble(double min, double max) {
184  SecureRNG<uint64_t> srng;
185  return randDouble(min, max, srng);
186  }
187 
196  template <class RNG = DefaultGenerator, class /* EnableIf */ = ValidRNG<RNG>>
197  static void seed(RNG& rng);
198 
207  template <class RNG = DefaultGenerator, class /* EnableIf */ = ValidRNG<RNG>>
208  static RNG create();
209 
213  static uint32_t rand32() {
214  return rand32(ThreadLocalPRNG());
215  }
216 
220  template <class RNG, class /* EnableIf */ = ValidRNG<RNG>>
221  static uint32_t rand32(RNG&& rng) {
222  return rng();
223  }
224 
229  return rand32(0, max, ThreadLocalPRNG());
230  }
231 
236  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
237  static uint32_t rand32(uint32_t max, RNG&& rng) {
238  return rand32(0, max, rng);
239  }
240 
245  return rand32(min, max, ThreadLocalPRNG());
246  }
247 
252  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
253  static uint32_t rand32(uint32_t min, uint32_t max, RNG&& rng) {
254  if (min == max) {
255  return 0;
256  }
257  return std::uniform_int_distribution<uint32_t>(min, max - 1)(rng);
258  }
259 
263  static uint64_t rand64() {
264  return rand64(ThreadLocalPRNG());
265  }
266 
270  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
271  static uint64_t rand64(RNG&& rng) {
272  return ((uint64_t)rng() << 32) | rng();
273  }
274 
279  return rand64(0, max, ThreadLocalPRNG());
280  }
281 
285  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
286  static uint64_t rand64(uint64_t max, RNG&& rng) {
287  return rand64(0, max, rng);
288  }
289 
294  return rand64(min, max, ThreadLocalPRNG());
295  }
296 
300  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
301  static uint64_t rand64(uint64_t min, uint64_t max, RNG&& rng) {
302  if (min == max) {
303  return 0;
304  }
305  return std::uniform_int_distribution<uint64_t>(min, max - 1)(rng);
306  }
307 
311  static bool oneIn(uint32_t n) {
312  return oneIn(n, ThreadLocalPRNG());
313  }
314 
318  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
319  static bool oneIn(uint32_t n, RNG&& rng) {
320  if (n == 0) {
321  return false;
322  }
323  return rand32(0, n, rng) == 0;
324  }
325 
329  static double randDouble01() {
330  return randDouble01(ThreadLocalPRNG());
331  }
332 
336  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
337  static double randDouble01(RNG&& rng) {
338  return std::generate_canonical<double, std::numeric_limits<double>::digits>(
339  rng);
340  }
341 
345  static double randDouble(double min, double max) {
346  return randDouble(min, max, ThreadLocalPRNG());
347  }
348 
352  template <class RNG = ThreadLocalPRNG, class /* EnableIf */ = ValidRNG<RNG>>
353  static double randDouble(double min, double max, RNG&& rng) {
354  if (std::fabs(max - min) < std::numeric_limits<double>::epsilon()) {
355  return 0;
356  }
357  return std::uniform_real_distribution<double>(min, max)(rng);
358  }
359 };
360 
361 /*
362  * Return a good seed for a random number generator.
363  * Note that this is a legacy function, as it returns a 32-bit value, which
364  * is too small to be useful as a "real" RNG seed. Use the functions in class
365  * Random instead.
366  */
368  return Random::rand32();
369 }
370 
371 } // namespace folly
372 
373 #include <folly/Random-inl.h>
static uint64_t rand64(uint64_t min, uint64_t max)
Definition: Random.h:293
static uint64_t rand64(RNG &&rng)
Definition: Random.h:271
static constexpr result_type min()
Definition: Random.h:83
LogLevel max
Definition: LogLevel.cpp:31
static std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type secureRandom()
Definition: Random.h:112
PskType type
static const int seed
uint32_t result_type
Definition: Random.h:54
static double secureRandDouble01()
Definition: Random.h:175
static uint32_t secureRand32()
Definition: Random.h:121
static double secureRandDouble(double min, double max)
Definition: Random.h:183
static double randDouble01(RNG &&rng)
Definition: Random.h:337
double val
Definition: String.cpp:273
static uint32_t rand32(uint32_t min, uint32_t max, RNG &&rng)
Definition: Random.h:253
static uint64_t secureRand64(uint64_t max)
Definition: Random.h:151
static constexpr result_type max()
Definition: Random.h:61
typename std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type result_type
Definition: Random.h:77
folly::std T
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
typename std::enable_if< std::is_unsigned< invoke_result_t< RNG & >>::value, RNG >::type ValidRNG
Definition: Random.h:70
auto rng
Definition: CollectTest.cpp:31
static bool oneIn(uint32_t n, RNG &&rng)
Definition: Random.h:319
static double randDouble(double min, double max, RNG &&rng)
Definition: Random.h:353
static uint64_t rand64(uint64_t max)
Definition: Random.h:278
LogLevel min
Definition: LogLevel.cpp:30
static uint64_t secureRand64()
Definition: Random.h:144
static uint32_t secureRand32(uint32_t max)
Definition: Random.h:128
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
static constexpr result_type max()
Definition: Random.h:87
static const char *const value
Definition: Conv.cpp:50
static uint32_t rand32(RNG &&rng)
Definition: Random.h:221
static uint32_t secureRand32(uint32_t min, uint32_t max)
Definition: Random.h:136
static double randDouble01()
Definition: Random.h:329
std::mt19937 DefaultGenerator
Definition: Random.h:97
static double randDouble(double min, double max)
Definition: Random.h:345
result_type operator()()
Definition: Random.cpp:164
static constexpr result_type min()
Definition: Random.h:58
static uint32_t rand32(uint32_t min, uint32_t max)
Definition: Random.h:244
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
static uint32_t rand32(uint32_t max, RNG &&rng)
Definition: Random.h:237
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
static bool secureOneIn(uint32_t n)
Definition: Random.h:167
static uint32_t rand32(uint32_t max)
Definition: Random.h:228
static uint64_t secureRand64(uint64_t min, uint64_t max)
Definition: Random.h:159
uint32_t randomNumberSeed()
Definition: Random.h:367
static uint64_t rand64(uint64_t max, RNG &&rng)
Definition: Random.h:286
result_type operator()()
Definition: Random.h:79
static uint64_t rand64(uint64_t min, uint64_t max, RNG &&rng)
Definition: Random.h:301