From ca8622af3cd996c8480ab4187ffaee64b0828724 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 3 Feb 2003 10:29:47 +0000 Subject: Park & Miller PRNG can be safely initialized with any value but 0 and stuck at 0 as designed. Its BSD adaptation tries to fight it by mapping 0 to 2147483647 after calculation, but this method not works since 2147483647 seed returns to 0 again on the next interation. Instead of after calculation mapping, map 0 to another value _before_ calculation, so it never stucks. --- sys/libkern/random.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sys/libkern/random.c b/sys/libkern/random.c index 35ddea1..1145a84 100644 --- a/sys/libkern/random.c +++ b/sys/libkern/random.c @@ -61,11 +61,13 @@ random() * Park and Miller, Communications of the ACM, vol. 31, no. 10, * October 1988, p. 1195. */ - x = randseed; + /* Can't be initialized with 0, so use another value. */ + if ((x = randseed) == 0) + x = 123459876; hi = x / 127773; lo = x % 127773; t = 16807 * lo - 2836 * hi; - if (t <= 0) + if (t < 0) t += 0x7fffffff; randseed = t; return (t); -- cgit v1.1