| 45 | } |
| 46 | |
| 47 | uint64_t RandGenerator(uint64_t range) { |
| 48 | DCHECK_GT(range, 0u); |
| 49 | // We must discard random results above this number, as they would |
| 50 | // make the random generator non-uniform (consider e.g. if |
| 51 | // MAX_UINT64 was 7 and |range| was 5, then a result of 1 would be twice |
| 52 | // as likely as a result of 3 or 4). |
| 53 | uint64_t max_acceptable_value = |
| 54 | (std::numeric_limits<uint64_t>::max() / range) * range - 1; |
| 55 | |
| 56 | uint64_t value; |
| 57 | do { |
| 58 | value = butil::RandUint64(); |
| 59 | } while (value > max_acceptable_value); |
| 60 | |
| 61 | return value % range; |
| 62 | } |
| 63 | |
| 64 | std::string RandBytesAsString(size_t length) { |
| 65 | DCHECK_GT(length, 0u); |