2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_BASE_RANDOM_H_
18 #define FOLLY_BASE_RANDOM_H_
22 #include "folly/ThreadLocal.h"
27 * Return a good seed for a random number generator.
29 uint32_t randomNumberSeed();
34 * A PRNG with one instance per thread. This PRNG uses a mersenne twister random
35 * number generator and is seeded from /dev/urandom. It should not be used for
36 * anything which requires security, only for statistical randomness.
38 * An instance of this class represents the current threads PRNG. This means
39 * copying an instance of this class across threads will result in corruption
41 * Most users will use the Random class which implicitly creates this class.
42 * However, if you are worried about performance, you can memoize the TLS
43 * lookups that get the per thread state by manually using this class:
45 * ThreadLocalPRNG rng = Random::threadLocalPRNG()
47 * Random::rand32(rng);
50 class ThreadLocalPRNG {
52 typedef uint32_t result_type;
54 uint32_t operator()() {
55 // Using a static method allows the compiler to avoid allocating stack space
57 return getImpl(local_);
60 static constexpr result_type min() {
61 return std::numeric_limits<result_type>::min();
63 static constexpr result_type max() {
64 return std::numeric_limits<result_type>::max();
69 local_ = localInstance.get();
76 class LocalInstancePRNG;
77 static LocalInstancePRNG* initLocal();
78 static folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
81 static result_type getImpl(LocalInstancePRNG* local);
82 LocalInstancePRNG* local_;
91 using ValidRNG = typename std::enable_if<
92 std::is_unsigned<typename std::result_of<RNG&()>::type>::value,
98 * Returns a random uint32_t
100 template<class RNG = ThreadLocalPRNG>
101 static uint32_t rand32(ValidRNG<RNG> rrng = RNG()) {
102 uint32_t r = rrng.operator()();
107 * Returns a random uint32_t in [0, max). If max == 0, returns 0.
109 template<class RNG = ThreadLocalPRNG>
110 static uint32_t rand32(uint32_t max, ValidRNG<RNG> rng = RNG()) {
115 return std::uniform_int_distribution<uint32_t>(0, max - 1)(rng);
119 * Returns a random uint32_t in [min, max). If min == max, returns 0.
121 template<class RNG = ThreadLocalPRNG>
122 static uint32_t rand32(uint32_t min,
124 ValidRNG<RNG> rng = RNG()) {
129 return std::uniform_int_distribution<uint32_t>(min, max - 1)(rng);
133 * Returns a random uint64_t
135 template<class RNG = ThreadLocalPRNG>
136 static uint64_t rand64(ValidRNG<RNG> rng = RNG()) {
137 return ((uint64_t) rng() << 32) | rng();
141 * Returns a random uint64_t in [0, max). If max == 0, returns 0.
143 template<class RNG = ThreadLocalPRNG>
144 static uint64_t rand64(uint64_t max, ValidRNG<RNG> rng = RNG()) {
149 return std::uniform_int_distribution<uint64_t>(0, max - 1)(rng);
153 * Returns a random uint64_t in [min, max). If min == max, returns 0.
155 template<class RNG = ThreadLocalPRNG>
156 static uint64_t rand64(uint64_t min,
158 ValidRNG<RNG> rng = RNG()) {
163 return std::uniform_int_distribution<uint64_t>(min, max - 1)(rng);
167 * Returns true 1/n of the time. If n == 0, always returns false
169 template<class RNG = ThreadLocalPRNG>
170 static bool oneIn(uint32_t n, ValidRNG<RNG> rng = RNG()) {
175 return rand32(n, rng) == 0;
179 * Returns a double in [0, 1)
181 template<class RNG = ThreadLocalPRNG>
182 static double randDouble01(ValidRNG<RNG> rng = RNG()) {
183 return std::generate_canonical<double, std::numeric_limits<double>::digits>
188 * Returns a double in [min, max), if min == max, returns 0.
190 template<class RNG = ThreadLocalPRNG>
191 static double randDouble(double min, double max, ValidRNG<RNG> rng = RNG()) {
192 if (std::fabs(max - min) < std::numeric_limits<double>::epsilon()) {
195 return std::uniform_real_distribution<double>(min, max)(rng);