X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.h;h=6af65e95064dde5f726edfa040dff05069aa180c;hb=19db503e08e4ea46a8b4d9a272605006b6245f88;hp=3cb553da66d5e9d9ce34d7c5f2be57b9fdfffc80;hpb=f2987ecd74d921af398f7592762fbf1ec67bab1a;p=folly.git diff --git a/folly/Random.h b/folly/Random.h index 3cb553da..6af65e95 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,13 @@ #pragma once #define FOLLY_RANDOM_H_ -#include +#include +#include #include -#include +#include + #include +#include #if FOLLY_HAVE_EXTRANDOM_SFMT19937 #include @@ -40,16 +43,16 @@ namespace folly { * However, if you are worried about performance, you can memoize the TLS * lookups that get the per thread state by manually using this class: * - * ThreadLocalPRNG rng = Random::threadLocalPRNG() + * ThreadLocalPRNG rng; * for (...) { * Random::rand32(rng); * } */ class ThreadLocalPRNG { public: - typedef uint32_t result_type; + using result_type = uint32_t; - uint32_t operator()() { + result_type operator()() { // Using a static method allows the compiler to avoid allocating stack space // for this class. return getImpl(local_); @@ -72,15 +75,33 @@ class ThreadLocalPRNG { LocalInstancePRNG* local_; }; - class Random { - private: template using ValidRNG = typename std::enable_if< std::is_unsigned::type>::value, RNG>::type; + template + class SecureRNG { + public: + using result_type = typename std::enable_if< + std::is_integral::value && !std::is_same::value, + T>::type; + + result_type operator()() { + return Random::secureRandom(); + } + + static constexpr result_type min() { + return std::numeric_limits::min(); + } + + static constexpr result_type max() { + return std::numeric_limits::max(); + } + }; + public: // Default generator type. #if FOLLY_HAVE_EXTRANDOM_SFMT19937 @@ -99,14 +120,84 @@ class Random { */ template static typename std::enable_if< - std::is_integral::value && !std::is_same::value, - T>::type + std::is_integral::value && !std::is_same::value, + T>::type secureRandom() { T val; secureRandom(&val, sizeof(val)); return val; } + /** + * Returns a secure random uint32_t + */ + static uint32_t secureRand32() { + return secureRandom(); + } + + /** + * Returns a secure random uint32_t in [0, max). If max == 0, returns 0. + */ + static uint32_t secureRand32(uint32_t max) { + SecureRNG srng; + return rand32(max, srng); + } + + /** + * Returns a secure random uint32_t in [min, max). If min == max, returns 0. + */ + static uint32_t secureRand32(uint32_t min, uint32_t max) { + SecureRNG srng; + return rand32(min, max, srng); + } + + /** + * Returns a secure random uint64_t + */ + static uint64_t secureRand64() { + return secureRandom(); + } + + /** + * Returns a secure random uint64_t in [0, max). If max == 0, returns 0. + */ + static uint64_t secureRand64(uint64_t max) { + SecureRNG srng; + return rand64(max, srng); + } + + /** + * Returns a secure random uint64_t in [min, max). If min == max, returns 0. + */ + static uint64_t secureRand64(uint64_t min, uint64_t max) { + SecureRNG srng; + return rand64(min, max, srng); + } + + /** + * Returns true 1/n of the time. If n == 0, always returns false + */ + static bool secureOneIn(uint32_t n) { + SecureRNG srng; + return rand32(0, n, srng) == 0; + } + + /** + * Returns a secure double in [0, 1) + */ + static double secureRandDouble01() { + SecureRNG srng; + return randDouble01(srng); + } + + /** + * Returns a secure double in [min, max), if min == max, returns 0. + */ + static double secureRandDouble(double min, double max) { + SecureRNG srng; + return randDouble(min, max, srng); + } + /** * (Re-)Seed an existing RNG with a good seed. * @@ -262,15 +353,15 @@ class Random { } /** - * Returns a double in [min, max), if min == max, returns 0. - */ + * Returns a double in [min, max), if min == max, returns 0. + */ static double randDouble(double min, double max) { return randDouble(min, max, ThreadLocalPRNG()); } /** - * Returns a double in [min, max), if min == max, returns 0. - */ + * Returns a double in [min, max), if min == max, returns 0. + */ template > static double randDouble(double min, double max, RNG&& rng) { if (std::fabs(max - min) < std::numeric_limits::epsilon()) { @@ -278,7 +369,6 @@ class Random { } return std::uniform_real_distribution(min, max)(rng); } - }; /* @@ -291,6 +381,6 @@ inline uint32_t randomNumberSeed() { return Random::rand32(); } -} +} // namespace folly #include