From: Ben Pfaff Date: Tue, 3 Feb 2004 07:47:46 +0000 (+0000) Subject: (rng_get_double) Fix always-returning-zero bug in my preferred way, X-Git-Tag: v0.4.0~382 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=957761c7598cdbd1140e08962cd6999c6ce9437f;p=pspp-builds.git (rng_get_double) Fix always-returning-zero bug in my preferred way, and at the same time make sure rounding doesn't bite us. --- 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