proxygen
folly::Random Class Reference

#include <Random.h>

Classes

class  SecureRNG
 

Public Types

typedef std::mt19937 DefaultGenerator
 

Public Member Functions

template<class RNG , class >
auto create () -> RNG
 

Static Public Member Functions

static void secureRandom (void *data, size_t len)
 
template<class T >
static std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type secureRandom ()
 
static uint32_t secureRand32 ()
 
static uint32_t secureRand32 (uint32_t max)
 
static uint32_t secureRand32 (uint32_t min, uint32_t max)
 
static uint64_t secureRand64 ()
 
static uint64_t secureRand64 (uint64_t max)
 
static uint64_t secureRand64 (uint64_t min, uint64_t max)
 
static bool secureOneIn (uint32_t n)
 
static double secureRandDouble01 ()
 
static double secureRandDouble (double min, double max)
 
template<class RNG = DefaultGenerator, class = ValidRNG<RNG>>
static void seed (RNG &rng)
 
template<class RNG = DefaultGenerator, class = ValidRNG<RNG>>
static RNG create ()
 
static uint32_t rand32 ()
 
template<class RNG , class = ValidRNG<RNG>>
static uint32_t rand32 (RNG &&rng)
 
static uint32_t rand32 (uint32_t max)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint32_t rand32 (uint32_t max, RNG &&rng)
 
static uint32_t rand32 (uint32_t min, uint32_t max)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint32_t rand32 (uint32_t min, uint32_t max, RNG &&rng)
 
static uint64_t rand64 ()
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t rand64 (RNG &&rng)
 
static uint64_t rand64 (uint64_t max)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t rand64 (uint64_t max, RNG &&rng)
 
static uint64_t rand64 (uint64_t min, uint64_t max)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t rand64 (uint64_t min, uint64_t max, RNG &&rng)
 
static bool oneIn (uint32_t n)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static bool oneIn (uint32_t n, RNG &&rng)
 
static double randDouble01 ()
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static double randDouble01 (RNG &&rng)
 
static double randDouble (double min, double max)
 
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static double randDouble (double min, double max, RNG &&rng)
 

Private Types

template<class RNG >
using ValidRNG = typename std::enable_if< std::is_unsigned< invoke_result_t< RNG & >>::value, RNG >::type
 

Detailed Description

Definition at line 66 of file Random.h.

Member Typedef Documentation

typedef std::mt19937 folly::Random::DefaultGenerator

Definition at line 97 of file Random.h.

template<class RNG >
using folly::Random::ValidRNG = typename std:: enable_if<std::is_unsigned<invoke_result_t<RNG&>>::value, RNG>::type
private

Definition at line 70 of file Random.h.

Member Function Documentation

template<class RNG , class >
auto folly::Random::create ( ) -> RNG

Definition at line 80 of file Random-inl.h.

References folly::test::begin(), folly::test::end(), s, and folly::detail::SeedData< RNG >::seedData.

80  {
81  detail::SeedData<RNG> sd;
82  std::seed_seq s(std::begin(sd.seedData), std::end(sd.seedData));
83  return RNG(s);
84 }
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
static set< string > s
template<class RNG = DefaultGenerator, class = ValidRNG<RNG>>
static RNG folly::Random::create ( )
static

Create a new RNG, seeded with a good seed.

Note that you should usually use ThreadLocalPRNG unless you need reproducibility (such as during a test), in which case you'd want to create a RNG with a good seed in production, and seed it yourself in test.

Referenced by folly::ThreadLocalPRNG::operator()().

static bool folly::Random::oneIn ( uint32_t  n)
inlinestatic

Returns true 1/n of the time. If n == 0, always returns false

Definition at line 311 of file Random.h.

Referenced by BENCHMARK(), folly::io::Codec::compress(), and folly::io::Codec::uncompress().

311  {
312  return oneIn(n, ThreadLocalPRNG());
313  }
static bool oneIn(uint32_t n)
Definition: Random.h:311
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static bool folly::Random::oneIn ( uint32_t  n,
RNG &&  rng 
)
inlinestatic

Returns true 1/n of the time. If n == 0, always returns false

Definition at line 319 of file Random.h.

319  {
320  if (n == 0) {
321  return false;
322  }
323  return rand32(0, n, rng) == 0;
324  }
auto rng
Definition: CollectTest.cpp:31
static uint32_t rand32()
Definition: Random.h:213
template<class RNG , class = ValidRNG<RNG>>
static uint32_t folly::Random::rand32 ( RNG &&  rng)
inlinestatic

Returns a random uint32_t given a specific RNG

Definition at line 221 of file Random.h.

References rng.

221  {
222  return rng();
223  }
auto rng
Definition: CollectTest.cpp:31
static uint32_t folly::Random::rand32 ( uint32_t  max)
inlinestatic

Returns a random uint32_t in [0, max). If max == 0, returns 0.

Definition at line 228 of file Random.h.

228  {
229  return rand32(0, max, ThreadLocalPRNG());
230  }
LogLevel max
Definition: LogLevel.cpp:31
static uint32_t rand32()
Definition: Random.h:213
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint32_t folly::Random::rand32 ( uint32_t  max,
RNG &&  rng 
)
inlinestatic

Returns a random uint32_t in [0, max) given a specific RNG. If max == 0, returns 0.

Definition at line 237 of file Random.h.

237  {
238  return rand32(0, max, rng);
239  }
LogLevel max
Definition: LogLevel.cpp:31
auto rng
Definition: CollectTest.cpp:31
static uint32_t rand32()
Definition: Random.h:213
static uint32_t folly::Random::rand32 ( uint32_t  min,
uint32_t  max 
)
inlinestatic

Returns a random uint32_t in [min, max). If min == max, returns 0.

Definition at line 244 of file Random.h.

244  {
245  return rand32(min, max, ThreadLocalPRNG());
246  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static uint32_t rand32()
Definition: Random.h:213
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint32_t folly::Random::rand32 ( uint32_t  min,
uint32_t  max,
RNG &&  rng 
)
inlinestatic

Returns a random uint32_t in [min, max) given a specific RNG. If min == max, returns 0.

Definition at line 253 of file Random.h.

References folly::ThreadLocalPRNG::min().

253  {
254  if (min == max) {
255  return 0;
256  }
257  return std::uniform_int_distribution<uint32_t>(min, max - 1)(rng);
258  }
LogLevel max
Definition: LogLevel.cpp:31
auto rng
Definition: CollectTest.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t folly::Random::rand64 ( RNG &&  rng)
inlinestatic

Returns a random uint64_t

Definition at line 271 of file Random.h.

References rng, and uint64_t.

271  {
272  return ((uint64_t)rng() << 32) | rng();
273  }
auto rng
Definition: CollectTest.cpp:31
static uint64_t folly::Random::rand64 ( uint64_t  max)
inlinestatic

Returns a random uint64_t in [0, max). If max == 0, returns 0.

Definition at line 278 of file Random.h.

278  {
279  return rand64(0, max, ThreadLocalPRNG());
280  }
LogLevel max
Definition: LogLevel.cpp:31
static uint64_t rand64()
Definition: Random.h:263
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t folly::Random::rand64 ( uint64_t  max,
RNG &&  rng 
)
inlinestatic

Returns a random uint64_t in [0, max). If max == 0, returns 0.

Definition at line 286 of file Random.h.

286  {
287  return rand64(0, max, rng);
288  }
LogLevel max
Definition: LogLevel.cpp:31
auto rng
Definition: CollectTest.cpp:31
static uint64_t rand64()
Definition: Random.h:263
static uint64_t folly::Random::rand64 ( uint64_t  min,
uint64_t  max 
)
inlinestatic

Returns a random uint64_t in [min, max). If min == max, returns 0.

Definition at line 293 of file Random.h.

293  {
294  return rand64(min, max, ThreadLocalPRNG());
295  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static uint64_t rand64()
Definition: Random.h:263
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static uint64_t folly::Random::rand64 ( uint64_t  min,
uint64_t  max,
RNG &&  rng 
)
inlinestatic

Returns a random uint64_t in [min, max). If min == max, returns 0.

Definition at line 301 of file Random.h.

References folly::ThreadLocalPRNG::min().

301  {
302  if (min == max) {
303  return 0;
304  }
305  return std::uniform_int_distribution<uint64_t>(min, max - 1)(rng);
306  }
LogLevel max
Definition: LogLevel.cpp:31
auto rng
Definition: CollectTest.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static double folly::Random::randDouble ( double  min,
double  max 
)
inlinestatic

Returns a double in [min, max), if min == max, returns 0.

Definition at line 345 of file Random.h.

Referenced by TEST().

345  {
346  return randDouble(min, max, ThreadLocalPRNG());
347  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static double randDouble(double min, double max)
Definition: Random.h:345
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static double folly::Random::randDouble ( double  min,
double  max,
RNG &&  rng 
)
inlinestatic

Returns a double in [min, max), if min == max, returns 0.

Definition at line 353 of file Random.h.

References folly::ThreadLocalPRNG::max(), and folly::ThreadLocalPRNG::min().

353  {
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  }
LogLevel max
Definition: LogLevel.cpp:31
auto rng
Definition: CollectTest.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static double folly::Random::randDouble01 ( )
inlinestatic

Returns a double in [0, 1)

Definition at line 329 of file Random.h.

Referenced by BENCHMARK().

329  {
330  return randDouble01(ThreadLocalPRNG());
331  }
static double randDouble01()
Definition: Random.h:329
template<class RNG = ThreadLocalPRNG, class = ValidRNG<RNG>>
static double folly::Random::randDouble01 ( RNG &&  rng)
inlinestatic

Returns a double in [0, 1)

Definition at line 337 of file Random.h.

References rng.

337  {
338  return std::generate_canonical<double, std::numeric_limits<double>::digits>(
339  rng);
340  }
auto rng
Definition: CollectTest.cpp:31
static bool folly::Random::secureOneIn ( uint32_t  n)
inlinestatic

Returns true 1/n of the time. If n == 0, always returns false

Definition at line 167 of file Random.h.

167  {
168  SecureRNG<uint32_t> srng;
169  return rand32(0, n, srng) == 0;
170  }
static uint32_t rand32()
Definition: Random.h:213
static uint32_t folly::Random::secureRand32 ( )
inlinestatic

Returns a secure random uint32_t

Definition at line 121 of file Random.h.

121  {
122  return secureRandom<uint32_t>();
123  }
static uint32_t folly::Random::secureRand32 ( uint32_t  max)
inlinestatic

Returns a secure random uint32_t in [0, max). If max == 0, returns 0.

Definition at line 128 of file Random.h.

128  {
129  SecureRNG<uint32_t> srng;
130  return rand32(max, srng);
131  }
LogLevel max
Definition: LogLevel.cpp:31
static uint32_t rand32()
Definition: Random.h:213
static uint32_t folly::Random::secureRand32 ( uint32_t  min,
uint32_t  max 
)
inlinestatic

Returns a secure random uint32_t in [min, max). If min == max, returns 0.

Definition at line 136 of file Random.h.

136  {
137  SecureRNG<uint32_t> srng;
138  return rand32(min, max, srng);
139  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static uint32_t rand32()
Definition: Random.h:213
static uint64_t folly::Random::secureRand64 ( )
inlinestatic

Returns a secure random uint64_t

Definition at line 144 of file Random.h.

144  {
145  return secureRandom<uint64_t>();
146  }
static uint64_t folly::Random::secureRand64 ( uint64_t  max)
inlinestatic

Returns a secure random uint64_t in [0, max). If max == 0, returns 0.

Definition at line 151 of file Random.h.

151  {
152  SecureRNG<uint64_t> srng;
153  return rand64(max, srng);
154  }
LogLevel max
Definition: LogLevel.cpp:31
static uint64_t rand64()
Definition: Random.h:263
static uint64_t folly::Random::secureRand64 ( uint64_t  min,
uint64_t  max 
)
inlinestatic

Returns a secure random uint64_t in [min, max). If min == max, returns 0.

Definition at line 159 of file Random.h.

159  {
160  SecureRNG<uint64_t> srng;
161  return rand64(min, max, srng);
162  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static uint64_t rand64()
Definition: Random.h:263
static double folly::Random::secureRandDouble ( double  min,
double  max 
)
inlinestatic

Returns a secure double in [min, max), if min == max, returns 0.

Definition at line 183 of file Random.h.

References rng, and seed.

183  {
184  SecureRNG<uint64_t> srng;
185  return randDouble(min, max, srng);
186  }
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
static double randDouble(double min, double max)
Definition: Random.h:345
static double folly::Random::secureRandDouble01 ( )
inlinestatic

Returns a secure double in [0, 1)

Definition at line 175 of file Random.h.

175  {
176  SecureRNG<uint64_t> srng;
177  return randDouble01(srng);
178  }
static double randDouble01()
Definition: Random.h:329
void Random::secureRandom ( void *  data,
size_t  len 
)
static

Get secure random bytes. (On Linux and OSX, this means /dev/urandom).

Definition at line 159 of file Random.cpp.

References folly::pushmi::operators::get.

159  {
160  using Single = SingletonThreadLocal<BufferedRandomDevice, RandomTag>;
161  Single::get().get(data, size);
162 }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
template<class T >
static std::enable_if< std::is_integral<T>::value && !std::is_same<T, bool>::value, T>::type folly::Random::secureRandom ( )
inlinestatic

Shortcut to get a secure random value of integral type.

Definition at line 112 of file Random.h.

References folly::T, and val.

Referenced by BENCHMARK(), proxygen::HTTP1xCodec::generateWebsocketKey(), folly::detail::SeedData< RNG >::SeedData(), wangle::ServerSocketConfig::ServerSocketConfig(), and TEST().

112  {
113  T val;
114  secureRandom(&val, sizeof(val));
115  return val;
116  }
static std::enable_if< std::is_integral< T >::value &&!std::is_same< T, bool >::value, T >::type secureRandom()
Definition: Random.h:112
double val
Definition: String.cpp:273
folly::std T
template<class RNG , class >
void Random::seed ( RNG &  rng)
static

(Re-)Seed an existing RNG with a good seed.

Note that you should usually use ThreadLocalPRNG unless you need reproducibility (such as during a test), in which case you'd want to create a RNG with a good seed in production, and seed it yourself in test.

Definition at line 73 of file Random-inl.h.

References folly::test::begin(), folly::test::end(), s, and folly::detail::SeedData< RNG >::seedData.

73  {
74  detail::SeedData<RNG> sd;
75  std::seed_seq s(std::begin(sd.seedData), std::end(sd.seedData));
76  rng.seed(s);
77 }
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto rng
Definition: CollectTest.cpp:31
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
static set< string > s

The documentation for this class was generated from the following files: