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