diff --git a/flow/DeterministicRandom.h b/flow/DeterministicRandom.h index f2e23fefca..369bbef010 100644 --- a/flow/DeterministicRandom.h +++ b/flow/DeterministicRandom.h @@ -24,6 +24,8 @@ #include #include "flow/IRandom.h" +#include "flow/Error.h" +#include "flow/Trace.h" #include @@ -90,6 +92,14 @@ public: uint32_t randomUInt32() { return gen64(); } + uint32_t randomSkewedUInt32(uint32_t min, uint32_t maxPlusOne) { + std::uniform_real_distribution distribution( std::log(min), std::log(maxPlusOne-1) ); + double logpower = distribution(random); + uint32_t loguniform = static_cast( std::pow( 10, logpower ) ); + // doubles can be imprecise, so let's make sure we don't violate an edge case. + return std::max(std::min(loguniform, maxPlusOne-1), min); + } + UID randomUniqueID() { uint64_t x,y; x = gen64(); diff --git a/flow/IRandom.h b/flow/IRandom.h index 3d957a004d..c5d60cb01d 100644 --- a/flow/IRandom.h +++ b/flow/IRandom.h @@ -76,6 +76,7 @@ public: virtual UID randomUniqueID() = 0; virtual char randomAlphaNumeric() = 0; virtual std::string randomAlphaNumeric( int length ) = 0; + virtual uint32_t randomSkewedUInt32(uint32_t min, uint32_t maxPlusOne) = 0; virtual uint64_t peek() const = 0; // returns something that is probably different for different random states. Deterministic (and idempotent) for a deterministic generator. // The following functions have fixed implementations for now: