From 03eb7511c9346495a50442c2be9311c101b9ccc6 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 5 Dec 2016 11:49:34 +0100 Subject: [PATCH] src/daemon/utils_random.[ch]: Implement cdrand_u(). --- src/daemon/utils_random.c | 13 ++++++++++++- src/daemon/utils_random.h | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) 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. -- 2.30.2