[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 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 | |||||
7 | #include <math.h> | ||||
8 | |||||
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 9 | #include <limits> |
10 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | #include "base/basictypes.h" |
12 | #include "base/logging.h" | ||||
13 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 14 | namespace base { |
15 | |||||
16 | int RandInt(int min, int max) { | ||||
[email protected] | d7a93ad | 2011-04-22 13:13:07 | [diff] [blame] | 17 | DCHECK_LE(min, max); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 18 | |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 19 | uint64 range = static_cast<uint64>(max) - min + 1; |
20 | int result = min + static_cast<int>(base::RandGenerator(range)); | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 21 | DCHECK(result >= min && result <= max); |
22 | return result; | ||||
23 | } | ||||
24 | |||||
25 | double RandDouble() { | ||||
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 26 | // We try to get maximum precision by masking out as many bits as will fit |
27 | // in the target type's mantissa, and raising it to an appropriate power to | ||||
28 | // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa | ||||
29 | // is expected to accommodate 53 bits. | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 30 | |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 31 | COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); |
32 | static const int kBits = std::numeric_limits<double>::digits; | ||||
[email protected] | ba99012 | 2008-11-14 23:28:29 | [diff] [blame] | 33 | uint64 random_bits = base::RandUint64() & ((GG_UINT64_C(1) << kBits) - 1); |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 34 | double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 35 | DCHECK(result >= 0.0 && result < 1.0); |
36 | return result; | ||||
37 | } | ||||
38 | |||||
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 39 | uint64 RandGenerator(uint64 max) { |
[email protected] | 88563f6 | 2011-03-13 22:13:33 | [diff] [blame] | 40 | DCHECK_GT(max, 0ULL); |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 41 | return base::RandUint64() % max; |
42 | } | ||||
43 | |||||
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame^] | 44 | std::string RandBytesAsString(size_t length) { |
45 | const size_t kBitsPerChar = 8; | ||||
46 | const int kCharsPerInt64 = sizeof(uint64)/sizeof(char); | ||||
47 | |||||
48 | std::string result(length, '\0'); | ||||
49 | uint64 entropy = 0; | ||||
50 | for (size_t i = 0; i < result.size(); ++i) { | ||||
51 | if (i % kCharsPerInt64 == 0) | ||||
52 | entropy = RandUint64(); | ||||
53 | result[i] = static_cast<char>(entropy); | ||||
54 | entropy >>= kBitsPerChar; | ||||
55 | } | ||||
56 | |||||
57 | return result; | ||||
58 | } | ||||
59 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 60 | } // namespace base |