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.
This commit is contained in:
Andrey A. Chernov 2003-02-03 10:29:47 +00:00
parent 2f5ef51de2
commit b413a2949e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=110281

View File

@ -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);