Implement g_random->randomSkewedUInt32()

This generates an exponential distribution, implemented as being
a "log-uniform" distribution.
This commit is contained in:
Alex Miller 2019-03-03 12:57:43 -08:00
parent 52d5a721a6
commit a462359af1
2 changed files with 11 additions and 0 deletions

View File

@ -24,6 +24,8 @@
#include <cinttypes>
#include "flow/IRandom.h"
#include "flow/Error.h"
#include "flow/Trace.h"
#include <random>
@ -90,6 +92,14 @@ public:
uint32_t randomUInt32() { return gen64(); }
uint32_t randomSkewedUInt32(uint32_t min, uint32_t maxPlusOne) {
std::uniform_real_distribution<double> distribution( std::log(min), std::log(maxPlusOne-1) );
double logpower = distribution(random);
uint32_t loguniform = static_cast<uint32_t>( 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();

View File

@ -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: