blob: 931eb4e943e646e2f39e2848b33683247b5b4725 [file] [log] [blame]
[email protected]d7a93ad2011-04-22 13:13:071// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]05f9b682008-09-29 22:18:012// 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>
tfarina32089922015-05-18 22:14:098#include <stdint.h>
[email protected]05f9b682008-09-29 22:18:019
[email protected]c56bef4d2013-10-15 20:29:0310#include <algorithm>
[email protected]94a0f312008-09-30 14:26:3311#include <limits>
12
[email protected]05f9b682008-09-29 22:18:0113#include "base/basictypes.h"
14#include "base/logging.h"
[email protected]c851cfd2013-06-10 20:11:1415#include "base/strings/string_util.h"
[email protected]05f9b682008-09-29 22:18:0116
[email protected]05f9b682008-09-29 22:18:0117namespace base {
18
19int RandInt(int min, int max) {
[email protected]d7a93ad2011-04-22 13:13:0720 DCHECK_LE(min, max);
[email protected]05f9b682008-09-29 22:18:0121
[email protected]a74dcae2010-08-30 21:07:0522 uint64 range = static_cast<uint64>(max) - min + 1;
23 int result = min + static_cast<int>(base::RandGenerator(range));
[email protected]e1be56d2011-05-04 01:29:3824 DCHECK_GE(result, min);
25 DCHECK_LE(result, max);
[email protected]05f9b682008-09-29 22:18:0126 return result;
27}
28
29double RandDouble() {
[email protected]edafd4c2011-05-10 17:18:5330 return BitsToOpenEndedUnitInterval(base::RandUint64());
31}
32
33double BitsToOpenEndedUnitInterval(uint64 bits) {
[email protected]94a0f312008-09-30 14:26:3334 // We try to get maximum precision by masking out as many bits as will fit
35 // in the target type's mantissa, and raising it to an appropriate power to
36 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
37 // is expected to accommodate 53 bits.
[email protected]05f9b682008-09-29 22:18:0138
[email protected]94a0f312008-09-30 14:26:3339 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
40 static const int kBits = std::numeric_limits<double>::digits;
tfarina32089922015-05-18 22:14:0941 uint64 random_bits = bits & ((UINT64_C(1) << kBits) - 1);
[email protected]94a0f312008-09-30 14:26:3342 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
[email protected]e1be56d2011-05-04 01:29:3843 DCHECK_GE(result, 0.0);
44 DCHECK_LT(result, 1.0);
[email protected]05f9b682008-09-29 22:18:0145 return result;
46}
47
[email protected]0173b962011-08-24 19:58:3648uint64 RandGenerator(uint64 range) {
49 DCHECK_GT(range, 0u);
[email protected]af2e192b2011-05-30 17:39:0950 // We must discard random results above this number, as they would
51 // make the random generator non-uniform (consider e.g. if
[email protected]0173b962011-08-24 19:58:3652 // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice
53 // as likely as a result of 3 or 4).
[email protected]af2e192b2011-05-30 17:39:0954 uint64 max_acceptable_value =
[email protected]0173b962011-08-24 19:58:3655 (std::numeric_limits<uint64>::max() / range) * range - 1;
[email protected]af2e192b2011-05-30 17:39:0956
57 uint64 value;
58 do {
59 value = base::RandUint64();
[email protected]0173b962011-08-24 19:58:3660 } while (value > max_acceptable_value);
[email protected]af2e192b2011-05-30 17:39:0961
[email protected]0173b962011-08-24 19:58:3662 return value % range;
[email protected]a74dcae2010-08-30 21:07:0563}
64
[email protected]51a01812011-05-05 08:46:1165std::string RandBytesAsString(size_t length) {
[email protected]fdce4782011-11-29 20:06:1866 DCHECK_GT(length, 0u);
[email protected]51a01812011-05-05 08:46:1167 std::string result;
68 RandBytes(WriteInto(&result, length + 1), length);
[email protected]29548d82011-04-29 21:03:5469 return result;
70}
71
[email protected]05f9b682008-09-29 22:18:0172} // namespace base