X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.h;h=0962d07e15445c6ae47385585073d680ea87ceed;hb=7beee001a4e416ae47da04c4a194d1bcc939c9d7;hp=53283a286a3d01f07d0a3e783562e7e64bcfdc04;hpb=321542683a01c3f334047531e9b487f047129775;p=folly.git diff --git a/folly/Random.h b/folly/Random.h index 53283a28..0962d07e 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -14,7 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_RANDOM_H_ +#pragma once #define FOLLY_RANDOM_H_ #include @@ -76,10 +76,10 @@ class ThreadLocalPRNG { class Random { private: - template + template using ValidRNG = typename std::enable_if< - std::is_unsigned::type>::value, - RNG>::type; + std::is_unsigned::type>::value, + RNG>::type; public: // Default generator type. @@ -115,8 +115,8 @@ class Random { * to create a RNG with a good seed in production, and seed it yourself * in test. */ - template - static void seed(ValidRNG& rng); + template > + static void seed(RNG& rng); /** * Create a new RNG, seeded with a good seed. @@ -126,14 +126,22 @@ class Random { * to create a RNG with a good seed in production, and seed it yourself * in test. */ - template - static ValidRNG create(); + template > + static RNG create(); /** * Returns a random uint32_t */ - template - static uint32_t rand32(ValidRNG rng = RNG()) { + static uint32_t rand32() { + ThreadLocalPRNG prng; + return rand32(prng); + } + + /** + * Returns a random uint32_t given a specific RNG + */ + template > + static uint32_t rand32(RNG rng) { uint32_t r = rng.operator()(); return r; } @@ -141,8 +149,17 @@ class Random { /** * Returns a random uint32_t in [0, max). If max == 0, returns 0. */ - template - static uint32_t rand32(uint32_t max, ValidRNG rng = RNG()) { + static uint32_t rand32(uint32_t max) { + ThreadLocalPRNG prng; + return rand32(max, prng); + } + + /** + * Returns a random uint32_t in [0, max) given a specific RNG. + * If max == 0, returns 0. + */ + template > + static uint32_t rand32(uint32_t max, RNG rng = RNG()) { if (max == 0) { return 0; } @@ -153,10 +170,8 @@ class Random { /** * Returns a random uint32_t in [min, max). If min == max, returns 0. */ - template - static uint32_t rand32(uint32_t min, - uint32_t max, - ValidRNG rng = RNG()) { + template > + static uint32_t rand32(uint32_t min, uint32_t max, RNG rng = RNG()) { if (min == max) { return 0; } @@ -167,16 +182,16 @@ class Random { /** * Returns a random uint64_t */ - template - static uint64_t rand64(ValidRNG rng = RNG()) { + template > + static uint64_t rand64(RNG rng = RNG()) { return ((uint64_t) rng() << 32) | rng(); } /** * Returns a random uint64_t in [0, max). If max == 0, returns 0. */ - template - static uint64_t rand64(uint64_t max, ValidRNG rng = RNG()) { + template > + static uint64_t rand64(uint64_t max, RNG rng = RNG()) { if (max == 0) { return 0; } @@ -187,10 +202,8 @@ class Random { /** * Returns a random uint64_t in [min, max). If min == max, returns 0. */ - template - static uint64_t rand64(uint64_t min, - uint64_t max, - ValidRNG rng = RNG()) { + template > + static uint64_t rand64(uint64_t min, uint64_t max, RNG rng = RNG()) { if (min == max) { return 0; } @@ -201,7 +214,7 @@ class Random { /** * Returns true 1/n of the time. If n == 0, always returns false */ - template + template > static bool oneIn(uint32_t n, ValidRNG rng = RNG()) { if (n == 0) { return false; @@ -213,8 +226,8 @@ class Random { /** * Returns a double in [0, 1) */ - template - static double randDouble01(ValidRNG rng = RNG()) { + template > + static double randDouble01(RNG rng = RNG()) { return std::generate_canonical::digits> (rng); } @@ -222,8 +235,8 @@ class Random { /** * Returns a double in [min, max), if min == max, returns 0. */ - template - static double randDouble(double min, double max, ValidRNG rng = RNG()) { + template > + static double randDouble(double min, double max, RNG rng = RNG()) { if (std::fabs(max - min) < std::numeric_limits::epsilon()) { return 0; } @@ -245,5 +258,3 @@ inline uint32_t randomNumberSeed() { } #include - -#endif