From aa8bbc12ddcb134defd9a0216cf15de00d3a5e5a Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 8 Mar 2020 18:01:10 +0100 Subject: [PATCH] MINOR: sample: make all bits random on the rand() sample fetch The rand() sample fetch supports being limited to a certain range, but it only uses 31 bits and scales them as requested, which means that when the requested output range is larger than 31 bits, the least significant one is not random and may even be constant. Let's make use of the whole 32 bits now that we have access ot them. --- src/sample.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sample.c b/src/sample.c index fd63902a9..088da418b 100644 --- a/src/sample.c +++ b/src/sample.c @@ -3124,11 +3124,11 @@ smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, voi static int smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private) { - smp->data.u.sint = ha_random(); + smp->data.u.sint = ha_random32(); /* reduce if needed. Don't do a modulo, use all bits! */ if (args && args[0].type == ARGT_SINT) - smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1); + smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32; smp->data.type = SMP_T_SINT; smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;