]> granicus.if.org Git - libevent/commit
Change evutil_weakrand_() to avoid platform random()
authorNicholas Marriott <nicholas.marriott@gmail.com>
Mon, 9 Apr 2012 14:46:32 +0000 (10:46 -0400)
committerNick Mathewson <nickm@torproject.org>
Mon, 9 Apr 2012 14:46:32 +0000 (10:46 -0400)
commite86af4b7e56ed5b7050cb4f41ae534f54748598c
tree8ca8da23c7ce08b9f803c87067246c283920aeb7
parentd9a55153366a6b842761e380d65b13fd70d9a507
Change evutil_weakrand_() to avoid platform random()

This change allows us to avoid perturbing the platform's random(), and
to avoid hitting locks on random() in the platform's libc.

evutil_weakrand_() is, well, weak, so we choose here an algorithm that
favors speed over a number of other possibly desirable properties.
We're using a linear congruential generator, and taking our parameters
from those shared by the OpenBSD random() implementation, and
Glibc's fastest random() implementation.

The low bits of a LCG of modulus 2^32 are (notoriously) less random
than the higher bits.  So to generate a random value in a range, using
the % operator is no good; we ought to divide.  We add an
evutil_weakrand_range_() function to do that.

This code also changes the interface of evutil_weakrand_() so that it
now manipulates an explicit seed, rather than having the seed in a
static variable.  This change enables us to use existing locks to
achieve thread-safety, rather than having to rely on an additional lock.

(Patch by Nicholas Marriott; commit message by Nick Mathewson.)
bufferevent-internal.h
bufferevent_ratelim.c
event-internal.h
evutil.c
poll.c
select.c
test/regress_buffer.c
test/regress_util.c
util-internal.h
win32select.c