X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FRandom.h;h=e74d64b14dcf441c944483092f7a6b8491fb4f2d;hp=0962d07e15445c6ae47385585073d680ea87ceed;hb=35054c8b8eefa0abaded33ff4150d195c9bf0e80;hpb=015f5dc525643e73a517cf22046ded72c5b6224d diff --git a/folly/Random.h b/folly/Random.h index 0962d07e..e74d64b1 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 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,7 +43,7 @@ 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); * } @@ -65,9 +68,9 @@ class ThreadLocalPRNG { ThreadLocalPRNG(); - private: class LocalInstancePRNG; + private: static result_type getImpl(LocalInstancePRNG* local); LocalInstancePRNG* local_; }; @@ -133,25 +136,22 @@ class Random { * Returns a random uint32_t */ static uint32_t rand32() { - ThreadLocalPRNG prng; - return rand32(prng); + return rand32(ThreadLocalPRNG()); } /** * Returns a random uint32_t given a specific RNG */ template > - static uint32_t rand32(RNG rng) { - uint32_t r = rng.operator()(); - return r; + static uint32_t rand32(RNG&& rng) { + return rng(); } /** * Returns a random uint32_t in [0, max). If max == 0, returns 0. */ static uint32_t rand32(uint32_t max) { - ThreadLocalPRNG prng; - return rand32(max, prng); + return rand32(0, max, ThreadLocalPRNG()); } /** @@ -159,84 +159,123 @@ class Random { * If max == 0, returns 0. */ template > - static uint32_t rand32(uint32_t max, RNG rng = RNG()) { - if (max == 0) { - return 0; - } - - return std::uniform_int_distribution(0, max - 1)(rng); + static uint32_t rand32(uint32_t max, RNG&& rng) { + return rand32(0, max, rng); } /** * Returns a random uint32_t in [min, max). If min == max, returns 0. */ + static uint32_t rand32(uint32_t min, uint32_t max) { + return rand32(min, max, ThreadLocalPRNG()); + } + + /** + * Returns a random uint32_t in [min, max) given a specific RNG. + * If min == max, returns 0. + */ template > - static uint32_t rand32(uint32_t min, uint32_t max, RNG rng = RNG()) { + static uint32_t rand32(uint32_t min, uint32_t max, RNG&& rng) { if (min == max) { return 0; } - return std::uniform_int_distribution(min, max - 1)(rng); } + /** + * Returns a random uint64_t + */ + static uint64_t rand64() { + return rand64(ThreadLocalPRNG()); + } + /** * Returns a random uint64_t */ template > - static uint64_t rand64(RNG rng = RNG()) { - return ((uint64_t) rng() << 32) | rng(); + static uint64_t rand64(RNG&& rng) { + return ((uint64_t)rng() << 32) | rng(); + } + + /** + * Returns a random uint64_t in [0, max). If max == 0, returns 0. + */ + static uint64_t rand64(uint64_t max) { + return rand64(0, max, ThreadLocalPRNG()); } /** * Returns a random uint64_t in [0, max). If max == 0, returns 0. */ template > - static uint64_t rand64(uint64_t max, RNG rng = RNG()) { - if (max == 0) { - return 0; - } + static uint64_t rand64(uint64_t max, RNG&& rng) { + return rand64(0, max, rng); + } - return std::uniform_int_distribution(0, max - 1)(rng); + /** + * Returns a random uint64_t in [min, max). If min == max, returns 0. + */ + static uint64_t rand64(uint64_t min, uint64_t max) { + return rand64(min, max, ThreadLocalPRNG()); } /** * Returns a random uint64_t in [min, max). If min == max, returns 0. */ template > - static uint64_t rand64(uint64_t min, uint64_t max, RNG rng = RNG()) { + static uint64_t rand64(uint64_t min, uint64_t max, RNG&& rng) { if (min == max) { return 0; } - return std::uniform_int_distribution(min, max - 1)(rng); } + /** + * Returns true 1/n of the time. If n == 0, always returns false + */ + static bool oneIn(uint32_t n) { + return oneIn(n, ThreadLocalPRNG()); + } + /** * Returns true 1/n of the time. If n == 0, always returns false */ template > - static bool oneIn(uint32_t n, ValidRNG rng = RNG()) { + static bool oneIn(uint32_t n, RNG&& rng) { if (n == 0) { return false; } + return rand32(0, n, rng) == 0; + } - return rand32(n, rng) == 0; + /** + * Returns a double in [0, 1) + */ + static double randDouble01() { + return randDouble01(ThreadLocalPRNG()); } /** * Returns a double in [0, 1) */ template > - static double randDouble01(RNG rng = RNG()) { - return std::generate_canonical::digits> - (rng); + static double randDouble01(RNG&& rng) { + return std::generate_canonical::digits>( + rng); + } + + /** + * 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. */ template > - static double randDouble(double min, double max, RNG rng = RNG()) { + static double randDouble(double min, double max, RNG&& rng) { if (std::fabs(max - min) < std::numeric_limits::epsilon()) { return 0; } @@ -255,6 +294,6 @@ inline uint32_t randomNumberSeed() { return Random::rand32(); } -} +} // namespace folly #include