Commit 46ad4f89 authored by gejun's avatar gejun

Add fast_rand_bytes in fast_rand.h

parent b3469340
......@@ -160,4 +160,20 @@ double fast_rand_double() {
return fast_rand_double(&_tls_seed);
}
void fast_rand_bytes(void* output, size_t output_length) {
const size_t n = output_length / 8;
for (size_t i = 0; i < n; ++i) {
static_cast<uint64_t*>(output)[i] = fast_rand();
}
const size_t m = output_length - n * 8;
if (m) {
uint8_t* p = static_cast<uint8_t*>(output) + n * 8;
uint64_t r = fast_rand();
for (size_t i = 0; i < m; ++i) {
p[i] = (r & 0xFF);
r = (r >> 8);
}
}
}
} // namespace butil
......@@ -63,6 +63,9 @@ template <typename T> T fast_rand_in(T min, T max) {
// Cost: ~15ns
double fast_rand_double();
// Fills |output_length| bytes of |output| with random data.
void fast_rand_bytes(void* output, size_t output_length, uint8_t min);
}
#endif // BUTIL_FAST_RAND_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment