MCPcopy Create free account
hub / github.com/apache/brpc / RandGenerator

Function RandGenerator

src/butil/rand_util.cc:47–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45}
46
47uint64_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
64std::string RandBytesAsString(size_t length) {
65 DCHECK_GT(length, 0u);

Callers 2

TESTFunction · 0.85
RandIntFunction · 0.85

Calls 1

RandUint64Function · 0.85

Tested by 1

TESTFunction · 0.68