From: Shuo Li Date: Tue, 4 Mar 2014 19:55:59 +0000 (-0800) Subject: Replace random_device/mt19937 with folly/Random X-Git-Tag: v0.22.0~659 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=920a14c76144ce1896c428ff41e00922b7baab38 Replace random_device/mt19937 with folly/Random Summary: Replace random_device/mt19937 with folly/Random to increase hygiene of using Random Test Plan: For each file is changed, fbmake -opt is run; except one file: stealing_test.cc, since the TARGETS file is not under the path. All fbmake are successfully passing Reviewed By: bmaurer@fb.com FB internal diff: D1199483 Blame Revision: v1 --- diff --git a/folly/Random.h b/folly/Random.h index f5874188..d10f2d64 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -114,6 +114,20 @@ class Random { return std::uniform_int_distribution(0, max - 1)(rng); } + /** + * 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()) { + if (min == max) { + return 0; + } + + return std::uniform_int_distribution(min, max - 1)(rng); + } + /** * Returns a random uint64_t */ @@ -134,6 +148,20 @@ class Random { return std::uniform_int_distribution(0, max - 1)(rng); } + /** + * 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()) { + 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 */ @@ -155,6 +183,17 @@ class Random { (rng); } + /** + * Returns a double in [min, max), if min == max, returns 0. + */ + template + static double randDouble(double min, double max, ValidRNG rng = RNG()) { + if (std::fabs(max - min) < std::numeric_limits::epsilon()) { + return 0; + } + return std::uniform_real_distribution(min, max)(rng); + } + }; }