rand: ensure that rand_between() can reach max value
authorJens Axboe <axboe@kernel.dk>
Tue, 12 Jun 2018 17:37:16 +0000 (11:37 -0600)
committerJens Axboe <axboe@kernel.dk>
Tue, 12 Jun 2018 17:37:16 +0000 (11:37 -0600)
We need to add 1, otherwise the maximum generated value will
be end -1. The API is both inclusive.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
lib/rand.h

index 4f10fb3c57afe6946e931b0f95233db3f68d6aaf..3554f6985e93ff02eacfb364054a5594007e88e5 100644 (file)
@@ -141,9 +141,9 @@ static inline uint64_t rand_between(struct frand_state *state, uint64_t start,
                                    uint64_t end)
 {
        if (state->use64)
-               return start + rand64_upto(state, end - start);
+               return start + rand64_upto(state, 1 + end - start);
        else
-               return start + rand32_upto(state, end - start);
+               return start + rand32_upto(state, 1 + end - start);
 }
 
 extern void init_rand(struct frand_state *, bool);