From: Florian Forster Date: Mon, 5 Dec 2016 10:49:34 +0000 (+0100) Subject: src/daemon/utils_random.[ch]: Implement cdrand_u(). X-Git-Tag: collectd-5.7.0~2^2~2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=03eb7511c9346495a50442c2be9311c101b9ccc6;p=collectd.git src/daemon/utils_random.[ch]: Implement cdrand_u(). --- diff --git a/src/daemon/utils_random.c b/src/daemon/utils_random.c index 34cf5b80..d490986d 100644 --- a/src/daemon/utils_random.c +++ b/src/daemon/utils_random.c @@ -50,7 +50,7 @@ static void cdrand_seed(void) { have_seed = 1; } -double cdrand_d(void) { +double cdrand_d() { double r; pthread_mutex_lock(&lock); @@ -61,6 +61,17 @@ double cdrand_d(void) { return (r); } +uint32_t cdrand_u() { + long r; + + pthread_mutex_lock(&lock); + cdrand_seed(); + r = jrand48(seed); + pthread_mutex_unlock(&lock); + + return (uint32_t)r; +} + long cdrand_range(long min, long max) { long range; long r; diff --git a/src/daemon/utils_random.h b/src/daemon/utils_random.h index d56bcf6a..e25ae9b6 100644 --- a/src/daemon/utils_random.h +++ b/src/daemon/utils_random.h @@ -29,7 +29,15 @@ * * This function is thread- and reentrant-safe. */ -double cdrand_d(void); +double cdrand_d(); + +/** + * cdrand_u returns a random uint32_t value uniformly distributed in the range + * [0-2^32). + * + * This function is thread- and reentrant-safe. + */ +uint32_t cdrand_u(); /** * Returns a random long between min and max, inclusively.