]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: make all bits random on the rand() sample fetch
authorWilly Tarreau <w@1wt.eu>
Sun, 8 Mar 2020 17:01:10 +0000 (18:01 +0100)
committerWilly Tarreau <w@1wt.eu>
Sun, 8 Mar 2020 17:04:16 +0000 (18:04 +0100)
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

index fd63902a9f5a6bc966cd673bbc56f5bc104273b0..088da418b8ca609cb1350a832e9855a67616048f 100644 (file)
@@ -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;