rand: use bools
[fio.git] / lib / rand.h
index 3d78115d48580f3ea5a0afc3e6e4cbcd7206e83a..24fac23cfec0856b409405e69a313dc7e873be33 100644 (file)
@@ -2,6 +2,7 @@
 #define FIO_RAND_H
 
 #include <inttypes.h>
+#include "types.h"
 #include "../arch/arch.h"
 
 #define FRAND32_MAX    (-1U)
@@ -109,16 +110,27 @@ static inline double __rand_0_1(struct frand_state *state)
        if (state->use64) {
                uint64_t val = __rand64(&state->state64);
 
-               return (double) val / (FRAND64_MAX + 1.0);
+               return (val + 1.0) / (FRAND64_MAX + 1.0);
        } else {
                uint32_t val = __rand32(&state->state32);
 
-               return (double) val / (FRAND32_MAX + 1.0);
+               return (val + 1.0) / (FRAND32_MAX + 1.0);
        }
 }
 
-extern void init_rand(struct frand_state *, int);
-extern void init_rand_seed(struct frand_state *, unsigned int seed, int);
+/*
+ * Generate a random value between 'start' and 'end', both inclusive
+ */
+static inline int rand_between(struct frand_state *state, int start, int end)
+{
+       uint64_t r;
+
+       r = __rand(state);
+       return start + (int) ((double)end * (r / (rand_max(state) + 1.0)));
+}
+
+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);
 extern unsigned long fill_random_buf(struct frand_state *, void *buf, unsigned int len);
 extern void __fill_random_buf_percentage(unsigned long, void *, unsigned int, unsigned int, unsigned int, char *, unsigned int);