From 957761c7598cdbd1140e08962cd6999c6ce9437f Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 3 Feb 2004 07:47:46 +0000 Subject: [PATCH] (rng_get_double) Fix always-returning-zero bug in my preferred way, and at the same time make sure rounding doesn't bite us. --- src/ChangeLog | 6 ++++++ src/random.c | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index aa539583..61713e2a 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +Mon Jan 19 14:08:09 2004 Ben Pfaff + + * random.c (rng_get_double): Fix always-returning-zero bug in my + preferred way, and at the same time make sure rounding doesn't + bite us. + Thu Jan 1 23:16:41 2004 Ben Pfaff * html.c: (change_attributes) Dead code, removed. diff --git a/src/random.c b/src/random.c index 163986cc..20a70fae 100644 --- a/src/random.c +++ b/src/random.c @@ -159,10 +159,16 @@ rng_get_unsigned (struct rng *rng) double rng_get_double (struct rng *rng) { - unsigned long value; - - rng_get_bytes (rng, &value, sizeof value); - return value / (double) ULONG_MAX; + for (;;) + { + unsigned long ulng; + double dbl; + + rng_get_bytes (rng, &ulng, sizeof ulng); + dbl = ulng / (ULONG_MAX + 1.0); + if (dbl >= 0 && dbl < 1) + return dbl; + } } /* Returns a random number from the distribution with mean 0 and -- 2.30.2