blob: c02c87509bc09fd336a41c08e8c403d2013837ab [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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
Alex Vakulenko674f0eb2016-01-20 08:10:48 -08007#include <limits.h>
Vitaly Bukacbed2062015-08-17 12:54:05 -07008#include <math.h>
9#include <stdint.h>
10
11#include <algorithm>
12#include <limits>
13
Vitaly Bukacbed2062015-08-17 12:54:05 -070014#include "base/logging.h"
15#include "base/strings/string_util.h"
16
17namespace base {
18
19int RandInt(int min, int max) {
20 DCHECK_LE(min, max);
21
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080022 uint64_t range = static_cast<uint64_t>(max) - min + 1;
23 // |range| is at most UINT_MAX + 1, so the result of RandGenerator(range)
24 // is at most UINT_MAX. Hence it's safe to cast it from uint64_t to int64_t.
25 int result =
26 static_cast<int>(min + static_cast<int64_t>(base::RandGenerator(range)));
Vitaly Bukacbed2062015-08-17 12:54:05 -070027 DCHECK_GE(result, min);
28 DCHECK_LE(result, max);
29 return result;
30}
31
32double RandDouble() {
33 return BitsToOpenEndedUnitInterval(base::RandUint64());
34}
35
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080036double BitsToOpenEndedUnitInterval(uint64_t bits) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070037 // We try to get maximum precision by masking out as many bits as will fit
38 // in the target type's mantissa, and raising it to an appropriate power to
39 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
40 // is expected to accommodate 53 bits.
41
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080042 static_assert(std::numeric_limits<double>::radix == 2,
43 "otherwise use scalbn");
Vitaly Bukacbed2062015-08-17 12:54:05 -070044 static const int kBits = std::numeric_limits<double>::digits;
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080045 uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1);
Vitaly Bukacbed2062015-08-17 12:54:05 -070046 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
47 DCHECK_GE(result, 0.0);
48 DCHECK_LT(result, 1.0);
49 return result;
50}
51
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080052uint64_t RandGenerator(uint64_t range) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070053 DCHECK_GT(range, 0u);
54 // We must discard random results above this number, as they would
55 // make the random generator non-uniform (consider e.g. if
56 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
57 // as likely as a result of 3 or 4).
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080058 uint64_t max_acceptable_value =
59 (std::numeric_limits<uint64_t>::max() / range) * range - 1;
Vitaly Bukacbed2062015-08-17 12:54:05 -070060
Alex Vakulenko674f0eb2016-01-20 08:10:48 -080061 uint64_t value;
Vitaly Bukacbed2062015-08-17 12:54:05 -070062 do {
63 value = base::RandUint64();
64 } while (value > max_acceptable_value);
65
66 return value % range;
67}
68
69std::string RandBytesAsString(size_t length) {
70 DCHECK_GT(length, 0u);
Vitaly Buka8750b272015-08-18 18:39:08 -070071 std::string result(length, ' ');
72 RandBytes(&result[0], length);
Vitaly Bukacbed2062015-08-17 12:54:05 -070073 return result;
74}
75
76} // namespace base