Commit 920a14c7 authored by Shuo Li's avatar Shuo Li Committed by Dave Watson

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
parent 60c0047a
......@@ -114,6 +114,20 @@ class Random {
return std::uniform_int_distribution<uint32_t>(0, max - 1)(rng);
}
/**
* Returns a random uint32_t in [min, max). If min == max, returns 0.
*/
template<class RNG = ThreadLocalPRNG>
static uint32_t rand32(uint32_t min,
uint32_t max,
ValidRNG<RNG> rng = RNG()) {
if (min == max) {
return 0;
}
return std::uniform_int_distribution<uint32_t>(min, max - 1)(rng);
}
/**
* Returns a random uint64_t
*/
......@@ -134,6 +148,20 @@ class Random {
return std::uniform_int_distribution<uint64_t>(0, max - 1)(rng);
}
/**
* Returns a random uint64_t in [min, max). If min == max, returns 0.
*/
template<class RNG = ThreadLocalPRNG>
static uint64_t rand64(uint64_t min,
uint64_t max,
ValidRNG<RNG> rng = RNG()) {
if (min == max) {
return 0;
}
return std::uniform_int_distribution<uint64_t>(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<class RNG = ThreadLocalPRNG>
static double randDouble(double min, double max, ValidRNG<RNG> rng = RNG()) {
if (std::fabs(max - min) < std::numeric_limits<double>::epsilon()) {
return 0;
}
return std::uniform_real_distribution<double>(min, max)(rng);
}
};
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment