summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2003-02-03 10:22:12 +0000
committerache <ache@FreeBSD.org>2003-02-03 10:22:12 +0000
commit2c36f3e337e908559979be58a51470c7336cab06 (patch)
treed0cfc71d1b2ec12652d4f2afb24ec1c659571650 /lib/libc/stdlib
parent35939fc6c18bf7f610fb321d5196e77794d86853 (diff)
downloadFreeBSD-src-2c36f3e337e908559979be58a51470c7336cab06.zip
FreeBSD-src-2c36f3e337e908559979be58a51470c7336cab06.tar.gz
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.
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/rand.c5
-rw-r--r--lib/libc/stdlib/random.c5
2 files changed, 8 insertions, 2 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 5b81da2..47bb36d 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -72,10 +72,13 @@ do_rand(unsigned long *ctx)
*/
long hi, lo, x;
+ /* Can't be initialized with 0, so use another value. */
+ if (*ctx == 0)
+ *ctx = 123459876;
hi = *ctx / 127773;
lo = *ctx % 127773;
x = 16807 * lo - 2836 * hi;
- if (x <= 0)
+ if (x < 0)
x += 0x7fffffff;
return ((*ctx = x) % ((u_long)RAND_MAX + 1));
#endif /* !USE_WEAK_SEEDING */
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c
index b3a1e11..82af94c 100644
--- a/lib/libc/stdlib/random.c
+++ b/lib/libc/stdlib/random.c
@@ -236,10 +236,13 @@ static inline long good_rand (x)
*/
long hi, lo;
+ /* Can't be initialized with 0, so use another value. */
+ if (x == 0)
+ x = 123459876;
hi = x / 127773;
lo = x % 127773;
x = 16807 * lo - 2836 * hi;
- if (x <= 0)
+ if (x < 0)
x += 0x7fffffff;
return (x);
#endif /* !USE_WEAK_SEEDING */
OpenPOWER on IntegriCloud