rand_util.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/rand_util.h"
  5. #include <limits.h>
  6. #include <math.h>
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <limits>
  10. #include "base/check_op.h"
  11. #include "base/strings/string_util.h"
  12. namespace base {
  13. uint64_t RandUint64() {
  14. uint64_t number;
  15. RandBytes(&number, sizeof(number));
  16. return number;
  17. }
  18. int RandInt(int min, int max) {
  19. DCHECK_LE(min, max);
  20. uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min) + 1;
  21. // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range)
  22. // is at most UINT_MAX. Hence it's safe to cast it from uint64_t to int64_t.
  23. int result =
  24. static_cast<int>(min + static_cast<int64_t>(base::RandGenerator(range)));
  25. DCHECK_GE(result, min);
  26. DCHECK_LE(result, max);
  27. return result;
  28. }
  29. double RandDouble() {
  30. return BitsToOpenEndedUnitInterval(base::RandUint64());
  31. }
  32. double BitsToOpenEndedUnitInterval(uint64_t bits) {
  33. // We try to get maximum precision by masking out as many bits as will fit
  34. // in the target type's mantissa, and raising it to an appropriate power to
  35. // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
  36. // is expected to accommodate 53 bits.
  37. static_assert(std::numeric_limits<double>::radix == 2,
  38. "otherwise use scalbn");
  39. static const int kBits = std::numeric_limits<double>::digits;
  40. uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1);
  41. double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
  42. DCHECK_GE(result, 0.0);
  43. DCHECK_LT(result, 1.0);
  44. return result;
  45. }
  46. uint64_t RandGenerator(uint64_t range) {
  47. DCHECK_GT(range, 0u);
  48. // We must discard random results above this number, as they would
  49. // make the random generator non-uniform (consider e.g. if
  50. // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
  51. // as likely as a result of 3 or 4).
  52. uint64_t max_acceptable_value =
  53. (std::numeric_limits<uint64_t>::max() / range) * range - 1;
  54. uint64_t value;
  55. do {
  56. value = base::RandUint64();
  57. } while (value > max_acceptable_value);
  58. return value % range;
  59. }
  60. std::string RandBytesAsString(size_t length) {
  61. DCHECK_GT(length, 0u);
  62. std::string result;
  63. RandBytes(WriteInto(&result, length + 1), length);
  64. return result;
  65. }
  66. InsecureRandomGenerator::InsecureRandomGenerator() {
  67. a_ = base::RandUint64();
  68. b_ = base::RandUint64();
  69. }
  70. void InsecureRandomGenerator::ReseedForTesting(uint64_t seed) {
  71. a_ = seed;
  72. b_ = seed;
  73. }
  74. uint64_t InsecureRandomGenerator::RandUint64() {
  75. // Using XorShift128+, which is simple and widely used. See
  76. // https://en.wikipedia.org/wiki/Xorshift#xorshift+ for details.
  77. uint64_t t = a_;
  78. const uint64_t s = b_;
  79. a_ = s;
  80. t ^= t << 23;
  81. t ^= t >> 17;
  82. t ^= s ^ (s >> 26);
  83. b_ = t;
  84. return t + s;
  85. }
  86. uint32_t InsecureRandomGenerator::RandUint32() {
  87. // The generator usually returns an uint64_t, truncate it.
  88. //
  89. // It is noted in this paper (https://arxiv.org/abs/1810.05313) that the
  90. // lowest 32 bits fail some statistical tests from the Big Crush
  91. // suite. Use the higher ones instead.
  92. return this->RandUint64() >> 32;
  93. }
  94. double InsecureRandomGenerator::RandDouble() {
  95. uint64_t x = RandUint64();
  96. // From https://vigna.di.unimi.it/xorshift/.
  97. // 53 bits of mantissa, hence the "hexadecimal exponent" 1p-53.
  98. return (x >> 11) * 0x1.0p-53;
  99. }
  100. MetricsSubSampler::MetricsSubSampler() = default;
  101. bool MetricsSubSampler::ShouldSample(double probability) {
  102. return generator_.RandDouble() < probability;
  103. }
  104. } // namespace base