random.h 5.4 KB

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