blob: 4140e9a539867026e856f50e670118378f2512ea [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>
8
[email protected]94a0f312008-09-30 14:26:339#include <limits>
10
[email protected]05f9b682008-09-29 22:18:0111#include "base/basictypes.h"
12#include "base/logging.h"
[email protected]51a01812011-05-05 08:46:1113#include "base/string_util.h"
[email protected]05f9b682008-09-29 22:18:0114
[email protected]05f9b682008-09-29 22:18:0115namespace base {
16
17int RandInt(int min, int max) {
[email protected]d7a93ad2011-04-22 13:13:0718 DCHECK_LE(min, max);
[email protected]05f9b682008-09-29 22:18:0119
[email protected]a74dcae2010-08-30 21:07:0520 uint64 range = static_cast<uint64>(max) - min + 1;
21 int result = min + static_cast<int>(base::RandGenerator(range));
[email protected]e1be56d2011-05-04 01:29:3822 DCHECK_GE(result, min);
23 DCHECK_LE(result, max);
[email protected]05f9b682008-09-29 22:18:0124 return result;
25}
26
27double RandDouble() {
[email protected]edafd4c2011-05-10 17:18:5328 return BitsToOpenEndedUnitInterval(base::RandUint64());
29}
30
31double BitsToOpenEndedUnitInterval(uint64 bits) {
[email protected]94a0f312008-09-30 14:26:3332 // We try to get maximum precision by masking out as many bits as will fit
33 // in the target type's mantissa, and raising it to an appropriate power to
34 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
35 // is expected to accommodate 53 bits.
[email protected]05f9b682008-09-29 22:18:0136
[email protected]94a0f312008-09-30 14:26:3337 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn);
38 static const int kBits = std::numeric_limits<double>::digits;
[email protected]edafd4c2011-05-10 17:18:5339 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1);
[email protected]94a0f312008-09-30 14:26:3340 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
[email protected]e1be56d2011-05-04 01:29:3841 DCHECK_GE(result, 0.0);
42 DCHECK_LT(result, 1.0);
[email protected]05f9b682008-09-29 22:18:0143 return result;
44}
45
[email protected]a74dcae2010-08-30 21:07:0546uint64 RandGenerator(uint64 max) {
[email protected]88563f62011-03-13 22:13:3347 DCHECK_GT(max, 0ULL);
[email protected]a74dcae2010-08-30 21:07:0548 return base::RandUint64() % max;
49}
50
[email protected]51a01812011-05-05 08:46:1151void RandBytes(void* output, size_t output_length) {
52 uint64 random_int;
53 size_t random_int_size = sizeof(random_int);
54 for (size_t i = 0; i < output_length; i += random_int_size) {
55 random_int = base::RandUint64();
56 size_t copy_count = std::min(output_length - i, random_int_size);
57 memcpy(((uint8*)output) + i, &random_int, copy_count);
[email protected]29548d82011-04-29 21:03:5458 }
[email protected]51a01812011-05-05 08:46:1159}
[email protected]29548d82011-04-29 21:03:5460
[email protected]51a01812011-05-05 08:46:1161std::string RandBytesAsString(size_t length) {
62 std::string result;
63 RandBytes(WriteInto(&result, length + 1), length);
[email protected]29548d82011-04-29 21:03:5464 return result;
65}
66
[email protected]05f9b682008-09-29 22:18:0167} // namespace base