]> git.ipfire.org Git - thirdparty/haproxy.git/commit
BUG/MEDIUM: sample: fix random number upper-bound
authorVincent Bernat <vincent@bernat.im>
Wed, 10 Dec 2014 09:31:37 +0000 (10:31 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 10 Dec 2014 21:45:34 +0000 (22:45 +0100)
commit1228dc0e7ae4a6b16c0c7f74a28f8e84601b526c
treefd5348f9453a5227a1de6a13e165ac87b7c728d2
parentf2dd68d0e047cdd433d698f044bb2d327886fc66
BUG/MEDIUM: sample: fix random number upper-bound

random() will generate a number between 0 and RAND_MAX. POSIX mandates
RAND_MAX to be at least 32767. GNU libc uses (1<<31 - 1) as
RAND_MAX.

In smp_fetch_rand(), a reduction is done with a multiply and shift to
avoid skewing the results. However, the shift was always 32 and hence
the numbers were not distributed uniformly in the specified range. We
fix that by dividing by RAND_MAX+1. gcc is smart enough to turn that
into a shift:

    0x000000000046ecc8 <+40>:    shr    $0x1f,%rax
src/sample.c