From cdfb5a85d9743fb53f4a2b56a392e0897a333568 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 12 Jun 2018 11:43:29 -0600 Subject: [PATCH] rand: make randX_upto() do the end value increment We should not do it in the caller, the functions that need fixing are really rand32/64_upto() instead. Also move a (now) misplaced comment. Fixes: c6fc6d2ab2c2 ("rand: ensure that rand_between() can reach max value") Signed-off-by: Jens Axboe --- lib/rand.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/rand.h b/lib/rand.h index 3554f698..1676cf98 100644 --- a/lib/rand.h +++ b/lib/rand.h @@ -114,9 +114,6 @@ static inline double __rand_0_1(struct frand_state *state) } } -/* - * Generate a random value between 'start' and 'end', both inclusive - */ static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) { uint32_t r; @@ -124,6 +121,7 @@ static inline uint32_t rand32_upto(struct frand_state *state, uint32_t end) assert(!state->use64); r = __rand32(&state->state32); + end++; return (int) ((double)end * (r / (FRAND32_MAX + 1.0))); } @@ -134,16 +132,20 @@ static inline uint64_t rand64_upto(struct frand_state *state, uint64_t end) assert(state->use64); r = __rand64(&state->state64); + end++; return (uint64_t) ((double)end * (r / (FRAND64_MAX + 1.0))); } +/* + * Generate a random value between 'start' and 'end', both inclusive + */ static inline uint64_t rand_between(struct frand_state *state, uint64_t start, uint64_t end) { if (state->use64) - return start + rand64_upto(state, 1 + end - start); + return start + rand64_upto(state, end - start); else - return start + rand32_upto(state, 1 + end - start); + return start + rand32_upto(state, end - start); } extern void init_rand(struct frand_state *, bool); -- 2.25.1