1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9 
10 #pragma once
11 #include <stdint.h>
12 #include <random>
13 
14 #include "rocksdb/rocksdb_namespace.h"
15 
16 namespace ROCKSDB_NAMESPACE {
17 
18 // A very simple random number generator.  Not especially good at
19 // generating truly random bits, but good enough for our needs in this
20 // package.
21 class Random {
22  private:
23   enum : uint32_t {
24     M = 2147483647L  // 2^31-1
25   };
26   enum : uint64_t {
27     A = 16807  // bits 14, 8, 7, 5, 2, 1, 0
28   };
29 
30   uint32_t seed_;
31 
GoodSeed(uint32_t s)32   static uint32_t GoodSeed(uint32_t s) { return (s & M) != 0 ? (s & M) : 1; }
33 
34  public:
35   // This is the largest value that can be returned from Next()
36   enum : uint32_t { kMaxNext = M };
37 
Random(uint32_t s)38   explicit Random(uint32_t s) : seed_(GoodSeed(s)) {}
39 
Reset(uint32_t s)40   void Reset(uint32_t s) { seed_ = GoodSeed(s); }
41 
Next()42   uint32_t Next() {
43     // We are computing
44     //       seed_ = (seed_ * A) % M,    where M = 2^31-1
45     //
46     // seed_ must not be zero or M, or else all subsequent computed values
47     // will be zero or M respectively.  For all other values, seed_ will end
48     // up cycling through every number in [1,M-1]
49     uint64_t product = seed_ * A;
50 
51     // Compute (product % M) using the fact that ((x << 31) % M) == x.
52     seed_ = static_cast<uint32_t>((product >> 31) + (product & M));
53     // The first reduction may overflow by 1 bit, so we may need to
54     // repeat.  mod == M is not possible; using > allows the faster
55     // sign-bit-based test.
56     if (seed_ > M) {
57       seed_ -= M;
58     }
59     return seed_;
60   }
61 
62   // Returns a uniformly distributed value in the range [0..n-1]
63   // REQUIRES: n > 0
Uniform(int n)64   uint32_t Uniform(int n) { return Next() % n; }
65 
66   // Randomly returns true ~"1/n" of the time, and false otherwise.
67   // REQUIRES: n > 0
OneIn(int n)68   bool OneIn(int n) { return Uniform(n) == 0; }
69 
70   // "Optional" one-in-n, where 0 or negative always returns false
71   // (may or may not consume a random value)
OneInOpt(int n)72   bool OneInOpt(int n) { return n > 0 && OneIn(n); }
73 
74   // Returns random bool that is true for the given percentage of
75   // calls on average. Zero or less is always false and 100 or more
76   // is always true (may or may not consume a random value)
PercentTrue(int percentage)77   bool PercentTrue(int percentage) {
78     return static_cast<int>(Uniform(100)) < percentage;
79   }
80 
81   // Skewed: pick "base" uniformly from range [0,max_log] and then
82   // return "base" random bits.  The effect is to pick a number in the
83   // range [0,2^max_log-1] with exponential bias towards smaller numbers.
Skewed(int max_log)84   uint32_t Skewed(int max_log) {
85     return Uniform(1 << Uniform(max_log + 1));
86   }
87 
88   // Returns a Random instance for use by the current thread without
89   // additional locking
90   static Random* GetTLSInstance();
91 };
92 
93 // A good 32-bit random number generator based on std::mt19937.
94 // This exists in part to avoid compiler variance in warning about coercing
95 // uint_fast32_t from mt19937 to uint32_t.
96 class Random32 {
97  private:
98   std::mt19937 generator_;
99 
100  public:
Random32(uint32_t s)101   explicit Random32(uint32_t s) : generator_(s) {}
102 
103   // Generates the next random number
Next()104   uint32_t Next() { return static_cast<uint32_t>(generator_()); }
105 
106   // Returns a uniformly distributed value in the range [0..n-1]
107   // REQUIRES: n > 0
Uniform(uint32_t n)108   uint32_t Uniform(uint32_t n) {
109     return static_cast<uint32_t>(
110         std::uniform_int_distribution<std::mt19937::result_type>(
111             0, n - 1)(generator_));
112   }
113 
114   // Returns an *almost* uniformly distributed value in the range [0..n-1].
115   // Much faster than Uniform().
116   // REQUIRES: n > 0
Uniformish(uint32_t n)117   uint32_t Uniformish(uint32_t n) {
118     // fastrange (without the header)
119     return static_cast<uint32_t>((uint64_t(generator_()) * uint64_t(n)) >> 32);
120   }
121 
122   // Randomly returns true ~"1/n" of the time, and false otherwise.
123   // REQUIRES: n > 0
OneIn(uint32_t n)124   bool OneIn(uint32_t n) { return Uniform(n) == 0; }
125 
126   // Skewed: pick "base" uniformly from range [0,max_log] and then
127   // return "base" random bits.  The effect is to pick a number in the
128   // range [0,2^max_log-1] with exponential bias towards smaller numbers.
Skewed(int max_log)129   uint32_t Skewed(int max_log) {
130     return Uniform(uint32_t{1} << Uniform(max_log + 1));
131   }
132 
133   // Reset the seed of the generator to the given value
Seed(uint32_t new_seed)134   void Seed(uint32_t new_seed) { generator_.seed(new_seed); }
135 };
136 
137 // A good 64-bit random number generator based on std::mt19937_64
138 class Random64 {
139  private:
140   std::mt19937_64 generator_;
141 
142  public:
Random64(uint64_t s)143   explicit Random64(uint64_t s) : generator_(s) { }
144 
145   // Generates the next random number
Next()146   uint64_t Next() { return generator_(); }
147 
148   // Returns a uniformly distributed value in the range [0..n-1]
149   // REQUIRES: n > 0
Uniform(uint64_t n)150   uint64_t Uniform(uint64_t n) {
151     return std::uniform_int_distribution<uint64_t>(0, n - 1)(generator_);
152   }
153 
154   // Randomly returns true ~"1/n" of the time, and false otherwise.
155   // REQUIRES: n > 0
OneIn(uint64_t n)156   bool OneIn(uint64_t n) { return Uniform(n) == 0; }
157 
158   // Skewed: pick "base" uniformly from range [0,max_log] and then
159   // return "base" random bits.  The effect is to pick a number in the
160   // range [0,2^max_log-1] with exponential bias towards smaller numbers.
Skewed(int max_log)161   uint64_t Skewed(int max_log) {
162     return Uniform(uint64_t(1) << Uniform(max_log + 1));
163   }
164 };
165 
166 }  // namespace ROCKSDB_NAMESPACE
167