blob: fab6c6613c2a9868310a66f9d8e71c689f310267 [file] [log] [blame]
kushi.p@gmail.come4869772011-04-22 22:13:07 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
mark@chromium.orgb93c0542008-09-30 07:18:01 +09002// 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
avia6a6a682015-12-27 07:15:14 +09007#include <limits.h>
mark@chromium.orgb93c0542008-09-30 07:18:01 +09008#include <math.h>
tfarina0dae6362015-05-19 07:14:09 +09009#include <stdint.h>
mark@chromium.orgb93c0542008-09-30 07:18:01 +090010
scottmg@chromium.orgfd621b92013-10-16 05:29:03 +090011#include <algorithm>
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090012#include <limits>
13
mark@chromium.orgb93c0542008-09-30 07:18:01 +090014#include "base/logging.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090015#include "base/strings/string_util.h"
mark@chromium.orgb93c0542008-09-30 07:18:01 +090016
mark@chromium.orgb93c0542008-09-30 07:18:01 +090017namespace base {
18
19int RandInt(int min, int max) {
kushi.p@gmail.come4869772011-04-22 22:13:07 +090020 DCHECK_LE(min, max);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090021
Nico Weber1796efe2015-10-30 05:42:58 +090022 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)));
kushi.p@gmail.com381e9662011-05-04 10:29:38 +090027 DCHECK_GE(result, min);
28 DCHECK_LE(result, max);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090029 return result;
30}
31
32double RandDouble() {
joi@chromium.orgdf3561d2011-05-11 02:18:53 +090033 return BitsToOpenEndedUnitInterval(base::RandUint64());
34}
35
Nico Weber1796efe2015-10-30 05:42:58 +090036double BitsToOpenEndedUnitInterval(uint64_t bits) {
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090037 // 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.
mark@chromium.orgb93c0542008-09-30 07:18:01 +090041
avi486c61f2015-11-24 23:26:24 +090042 static_assert(std::numeric_limits<double>::radix == 2,
43 "otherwise use scalbn");
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090044 static const int kBits = std::numeric_limits<double>::digits;
Nico Weber1796efe2015-10-30 05:42:58 +090045 uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1);
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090046 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
kushi.p@gmail.com381e9662011-05-04 10:29:38 +090047 DCHECK_GE(result, 0.0);
48 DCHECK_LT(result, 1.0);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090049 return result;
50}
51
Nico Weber1796efe2015-10-30 05:42:58 +090052uint64_t RandGenerator(uint64_t range) {
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090053 DCHECK_GT(range, 0u);
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090054 // We must discard random results above this number, as they would
55 // make the random generator non-uniform (consider e.g. if
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090056 // 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).
Nico Weber1796efe2015-10-30 05:42:58 +090058 uint64_t max_acceptable_value =
59 (std::numeric_limits<uint64_t>::max() / range) * range - 1;
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090060
Nico Weber1796efe2015-10-30 05:42:58 +090061 uint64_t value;
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090062 do {
63 value = base::RandUint64();
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090064 } while (value > max_acceptable_value);
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090065
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090066 return value % range;
isherman@chromium.org01eaa0b2010-08-31 06:07:05 +090067}
68
qsr@google.comfbda4d52011-05-05 17:46:11 +090069std::string RandBytesAsString(size_t length) {
pkasting@chromium.orgd3479372011-11-30 05:06:18 +090070 DCHECK_GT(length, 0u);
qsr@google.comfbda4d52011-05-05 17:46:11 +090071 std::string result;
72 RandBytes(WriteInto(&result, length + 1), length);
abarth@chromium.orgc19cb312011-04-30 06:03:54 +090073 return result;
74}
75
mark@chromium.orgb93c0542008-09-30 07:18:01 +090076} // namespace base