| 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 <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 (std::numeric_limits<uint64>::max() / range) * range - 1; | 54 (std::numeric_limits<uint64>::max() / range) * range - 1; |
| 55 | 55 |
| 56 uint64 value; | 56 uint64 value; |
| 57 do { | 57 do { |
| 58 value = base::RandUint64(); | 58 value = base::RandUint64(); |
| 59 } while (value > max_acceptable_value); | 59 } while (value > max_acceptable_value); |
| 60 | 60 |
| 61 return value % range; | 61 return value % range; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void RandBytes(void* output, size_t output_length) { | |
| 65 uint64 random_int; | |
| 66 size_t random_int_size = sizeof(random_int); | |
| 67 for (size_t i = 0; i < output_length; i += random_int_size) { | |
| 68 random_int = base::RandUint64(); | |
| 69 size_t copy_count = std::min(output_length - i, random_int_size); | |
| 70 memcpy(((uint8*)output) + i, &random_int, copy_count); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 std::string RandBytesAsString(size_t length) { | 64 std::string RandBytesAsString(size_t length) { |
| 75 DCHECK_GT(length, 0u); | 65 DCHECK_GT(length, 0u); |
| 76 std::string result; | 66 std::string result; |
| 77 RandBytes(WriteInto(&result, length + 1), length); | 67 RandBytes(WriteInto(&result, length + 1), length); |
| 78 return result; | 68 return result; |
| 79 } | 69 } |
| 80 | 70 |
| 81 } // namespace base | 71 } // namespace base |
| OLD | NEW |