and at the same time make sure rounding doesn't bite us.
+Mon Jan 19 14:08:09 2004 Ben Pfaff <blp@gnu.org>
+
+ * 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 <blp@gnu.org>
* html.c: (change_attributes) Dead code, removed.
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