2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_UTIL_RANDOM_H_
|
|
|
|
#define STORAGE_LEVELDB_UTIL_RANDOM_H_
|
|
|
|
|
2020-04-29 19:59:39 +00:00
|
|
|
#include <cstdint>
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
// A very simple random number generator. Not especially good at
|
|
|
|
// generating truly random bits, but good enough for our needs in this
|
|
|
|
// package.
|
|
|
|
class Random {
|
|
|
|
private:
|
|
|
|
uint32_t seed_;
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2013-08-21 11:12:47 -07:00
|
|
|
explicit Random(uint32_t s) : seed_(s & 0x7fffffffu) {
|
|
|
|
// Avoid bad seeds.
|
|
|
|
if (seed_ == 0 || seed_ == 2147483647L) {
|
|
|
|
seed_ = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
uint32_t Next() {
|
2019-05-02 11:01:00 -07:00
|
|
|
static const uint32_t M = 2147483647L; // 2^31-1
|
|
|
|
static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
|
2011-03-18 22:37:00 +00:00
|
|
|
// We are computing
|
|
|
|
// seed_ = (seed_ * A) % M, where M = 2^31-1
|
|
|
|
//
|
|
|
|
// seed_ must not be zero or M, or else all subsequent computed values
|
|
|
|
// will be zero or M respectively. For all other values, seed_ will end
|
|
|
|
// up cycling through every number in [1,M-1]
|
|
|
|
uint64_t product = seed_ * A;
|
|
|
|
|
|
|
|
// Compute (product % M) using the fact that ((x << 31) % M) == x.
|
2011-04-20 22:48:11 +00:00
|
|
|
seed_ = static_cast<uint32_t>((product >> 31) + (product & M));
|
2011-03-18 22:37:00 +00:00
|
|
|
// The first reduction may overflow by 1 bit, so we may need to
|
|
|
|
// repeat. mod == M is not possible; using > allows the faster
|
|
|
|
// sign-bit-based test.
|
|
|
|
if (seed_ > M) {
|
|
|
|
seed_ -= M;
|
|
|
|
}
|
|
|
|
return seed_;
|
|
|
|
}
|
|
|
|
// Returns a uniformly distributed value in the range [0..n-1]
|
|
|
|
// REQUIRES: n > 0
|
|
|
|
uint32_t Uniform(int n) { return Next() % n; }
|
|
|
|
|
|
|
|
// Randomly returns true ~"1/n" of the time, and false otherwise.
|
|
|
|
// REQUIRES: n > 0
|
|
|
|
bool OneIn(int n) { return (Next() % 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.
|
2019-05-02 11:01:00 -07:00
|
|
|
uint32_t Skewed(int max_log) { return Uniform(1 << Uniform(max_log + 1)); }
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_UTIL_RANDOM_H_
|