From 56c77c10f79d7be492aa0fb137786978c3596682 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 12 Jun 2018 08:10:47 -0600 Subject: [PATCH 1/1] rand: add rand64_between() For some reason we only had the 32-bit variant, and there's a use case for the 64-bit version. Add that, and add a 32/64 agnostic helper that can be called. Signed-off-by: Jens Axboe --- lib/rand.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/rand.h b/lib/rand.h index 8832c73a..6be53072 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -127,6 +127,24 @@ static inline int rand32_between(struct frand_state *state, int start, int end) return start + (int) ((double)end * (r / (FRAND32_MAX + 1.0))); } +static inline uint64_t rand64_between(struct frand_state *state, uint64_t start, + uint64_t end) +{ + uint64_t r; + + r = __rand64(&state->state64); + return start + (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); +} + +static inline uint64_t rand_between(struct frand_state *state, uint64_t start, + uint64_t end) +{ + if (state->use64) + return rand32_between(state, start, end); + else + return rand64_between(state, start, end); +} + extern void init_rand(struct frand_state *, bool); extern void init_rand_seed(struct frand_state *, unsigned int seed, bool); extern void __fill_random_buf(void *buf, unsigned int len, unsigned long seed); -- 2.25.1