From 043573b24fff014f9b5e8396006519240753754f Mon Sep 17 00:00:00 2001 From: Haobo Xu Date: Mon, 3 Jun 2013 23:23:03 -0700 Subject: [PATCH] [RocksDB] Include 64bit random number generator Summary: As title. Test Plan: make check; Reviewers: chip, MarkCallaghan CC: leveldb Differential Revision: https://reviews.facebook.net/D11061 --- util/random.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/util/random.h b/util/random.h index 07538242e..d64894fe8 100644 --- a/util/random.h +++ b/util/random.h @@ -5,6 +5,7 @@ #ifndef STORAGE_LEVELDB_UTIL_RANDOM_H_ #define STORAGE_LEVELDB_UTIL_RANDOM_H_ +#include #include namespace leveldb { @@ -54,6 +55,35 @@ class Random { } }; +// A simple 64bit random number generator based on std::mt19937_64 +class Random64 { + private: + std::mt19937_64 generator_; + + public: + explicit Random64(uint64_t s) : generator_(s) { } + + // Generates the next random number + uint64_t Next() { return generator_(); } + + // Returns a uniformly distributed value in the range [0..n-1] + // REQUIRES: n > 0 + uint64_t Uniform(uint64_t n) { + return std::uniform_int_distribution(0, n - 1)(generator_); + } + + // Randomly returns true ~"1/n" of the time, and false otherwise. + // REQUIRES: n > 0 + bool OneIn(uint64_t n) { return Uniform(n) == 0; } + + // Skewed: pick "base" uniformly from range [0,max_log] and then + // return "base" random bits. The effect is to pick a number in the + // range [0,2^max_log-1] with exponential bias towards smaller numbers. + uint64_t Skewed(int max_log) { + return Uniform(1 << Uniform(max_log + 1)); + } +}; + } // namespace leveldb #endif // STORAGE_LEVELDB_UTIL_RANDOM_H_