From: Willy Tarreau Date: Sun, 8 Mar 2020 17:01:10 +0000 (+0100) Subject: MINOR: sample: make all bits random on the rand() sample fetch X-Git-Tag: v2.2-dev4~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aa8bbc12ddcb134defd9a0216cf15de00d3a5e5a;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/src/sample.c b/src/sample.c index fd63902a9f..088da418b8 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;