OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/rand_util.h" | 5 #include "base/rand_util.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... | |
36 | 36 |
37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); | 37 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); |
38 static const int kBits = std::numeric_limits<double>::digits; | 38 static const int kBits = std::numeric_limits<double>::digits; |
39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); | 39 uint64 random_bits = bits & ((GG_UINT64_C(1) << kBits) - 1); |
40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); | 40 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); |
41 DCHECK_GE(result, 0.0); | 41 DCHECK_GE(result, 0.0); |
42 DCHECK_LT(result, 1.0); | 42 DCHECK_LT(result, 1.0); |
43 return result; | 43 return result; |
44 } | 44 } |
45 | 45 |
46 uint64 RandGenerator(uint64 max) { | 46 uint64 RandGenerator(uint64 n) { |
47 DCHECK_GT(max, 0ULL); | 47 DCHECK(n); |
Mark Mentovai
2011/08/23 17:01:33
The DCHECK_GT was a bit more expressive. Why did y
Denis Lagno
2011/08/24 12:58:28
Done.
| |
48 | 48 // We must discard random results above or equal this number, as they would |
Mark Mentovai
2011/08/23 17:01:33
Use correct English.
Denis Lagno
2011/08/24 12:58:28
Done.
| |
49 // We must discard random results above this number, as they would | |
50 // make the random generator non-uniform (consider e.g. if | 49 // make the random generator non-uniform (consider e.g. if |
51 // MAX_UINT64 was 4 and max was 3, then a result of 1 would be twice | 50 // MAX_UINT64 was 7 and |n| was 5, then a result of 1 would be twice |
52 // as likely as a result of 0 or 2). | 51 // as likely as a result of 3 or 4). |
53 uint64 max_acceptable_value = | 52 uint64 min_unacceptable_value = (std::numeric_limits<uint64>::max() / n) * n; |
54 (std::numeric_limits<uint64>::max() / max) * max; | |
55 | 53 |
56 uint64 value; | 54 uint64 value; |
57 do { | 55 do { |
58 value = base::RandUint64(); | 56 value = base::RandUint64(); |
59 } while (value >= max_acceptable_value); | 57 } while (value >= min_unacceptable_value); |
60 | 58 |
61 return value % max; | 59 return value % n; |
62 } | 60 } |
63 | 61 |
64 void RandBytes(void* output, size_t output_length) { | 62 void RandBytes(void* output, size_t output_length) { |
65 uint64 random_int; | 63 uint64 random_int; |
66 size_t random_int_size = sizeof(random_int); | 64 size_t random_int_size = sizeof(random_int); |
67 for (size_t i = 0; i < output_length; i += random_int_size) { | 65 for (size_t i = 0; i < output_length; i += random_int_size) { |
68 random_int = base::RandUint64(); | 66 random_int = base::RandUint64(); |
69 size_t copy_count = std::min(output_length - i, random_int_size); | 67 size_t copy_count = std::min(output_length - i, random_int_size); |
70 memcpy(((uint8*)output) + i, &random_int, copy_count); | 68 memcpy(((uint8*)output) + i, &random_int, copy_count); |
71 } | 69 } |
72 } | 70 } |
73 | 71 |
74 std::string RandBytesAsString(size_t length) { | 72 std::string RandBytesAsString(size_t length) { |
75 std::string result; | 73 std::string result; |
76 RandBytes(WriteInto(&result, length + 1), length); | 74 RandBytes(WriteInto(&result, length + 1), length); |
77 return result; | 75 return result; |
78 } | 76 } |
79 | 77 |
80 } // namespace base | 78 } // namespace base |
OLD | NEW |