blob: 1525b91449d438089a67b89f23521941a80e4500 [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
7#include <math.h>
8
scottmg@chromium.orgfd621b92013-10-16 05:29:03 +09009#include <algorithm>
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090010#include <limits>
11
mark@chromium.orgb93c0542008-09-30 07:18:01 +090012#include "base/basictypes.h"
13#include "base/logging.h"
avi@chromium.org68a745c2013-06-11 05:11:14 +090014#include "base/strings/string_util.h"
mark@chromium.orgb93c0542008-09-30 07:18:01 +090015
mark@chromium.orgb93c0542008-09-30 07:18:01 +090016namespace base {
17
18int RandInt(int min, int max) {
kushi.p@gmail.come4869772011-04-22 22:13:07 +090019 DCHECK_LE(min, max);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090020
isherman@chromium.org01eaa0b2010-08-31 06:07:05 +090021 uint64 range = static_cast<uint64>(max) - min + 1;
22 int result = min + static_cast<int>(base::RandGenerator(range));
kushi.p@gmail.com381e9662011-05-04 10:29:38 +090023 DCHECK_GE(result, min);
24 DCHECK_LE(result, max);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090025 return result;
26}
27
28double RandDouble() {
joi@chromium.orgdf3561d2011-05-11 02:18:53 +090029 return BitsToOpenEndedUnitInterval(base::RandUint64());
30}
31
32double BitsToOpenEndedUnitInterval(uint64 bits) {
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090033 // 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.
mark@chromium.orgb93c0542008-09-30 07:18:01 +090037
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090038 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
39 static const int kBits = std::numeric_limits<double>::digits;
joi@chromium.orgdf3561d2011-05-11 02:18:53 +090040 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1);
mark@chromium.orgc22c62b2008-09-30 23:26:33 +090041 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
kushi.p@gmail.com381e9662011-05-04 10:29:38 +090042 DCHECK_GE(result, 0.0);
43 DCHECK_LT(result, 1.0);
mark@chromium.orgb93c0542008-09-30 07:18:01 +090044 return result;
45}
46
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090047uint64 RandGenerator(uint64 range) {
48 DCHECK_GT(range, 0u);
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090049 // We must discard random results above this number, as they would
50 // make the random generator non-uniform (consider e.g. if
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090051 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
52 // as likely as a result of 3 or 4).
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090053 uint64 max_acceptable_value =
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090054 (std::numeric_limits<uint64>::max() / range) * range - 1;
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090055
56 uint64 value;
57 do {
58 value = base::RandUint64();
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090059 } while (value > max_acceptable_value);
joi@chromium.orgedf24fc2011-05-31 02:39:09 +090060
dilmah@chromium.orgbd365732011-08-25 04:58:36 +090061 return value % range;
isherman@chromium.org01eaa0b2010-08-31 06:07:05 +090062}
63
qsr@google.comfbda4d52011-05-05 17:46:11 +090064std::string RandBytesAsString(size_t length) {
pkasting@chromium.orgd3479372011-11-30 05:06:18 +090065 DCHECK_GT(length, 0u);
qsr@google.comfbda4d52011-05-05 17:46:11 +090066 std::string result;
67 RandBytes(WriteInto(&result, length + 1), length);
abarth@chromium.orgc19cb312011-04-30 06:03:54 +090068 return result;
69}
70
mark@chromium.orgb93c0542008-09-30 07:18:01 +090071} // namespace base