summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrey A. Chernov <ache@FreeBSD.org>2013-07-24 10:12:50 +0000
committerAndrey A. Chernov <ache@FreeBSD.org>2013-07-24 10:12:50 +0000
commite6ebddbe4bcd23f111674aee5339e4d43f1e04d8 (patch)
tree8383d0fa83abe6655d6cc65af5dcdd9484889e4e /lib
parent50dcab85bfec0d93f73ea9831c2a96207aea00fe (diff)
Notes
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdlib/rand.c41
1 files changed, 33 insertions, 8 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 0cbd94883125..8b12b1ab22f0 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -67,15 +67,15 @@ do_rand(unsigned long *ctx)
*/
long hi, lo, x;
- /* Can't be initialized with 0, so use another value. */
- if (*ctx == 0)
- *ctx = 123459876;
+ /* Must be in [1, 0x7ffffffe] range at this point. */
hi = *ctx / 127773;
lo = *ctx % 127773;
x = 16807 * lo - 2836 * hi;
if (x < 0)
x += 0x7fffffff;
- return ((*ctx = x) % ((u_long)RAND_MAX + 1));
+ *ctx = x;
+ /* Transform to [0, 0x7ffffffd] range. */
+ return (x - 1);
#endif /* !USE_WEAK_SEEDING */
}
@@ -83,15 +83,32 @@ do_rand(unsigned long *ctx)
int
rand_r(unsigned int *ctx)
{
- u_long val = (u_long) *ctx;
- int r = do_rand(&val);
+ u_long val;
+ int r;
- *ctx = (unsigned int) val;
+#ifdef USE_WEAK_SEEDING
+ val = *ctx;
+#else
+ /* Transform to [1, 0x7ffffffe] range. */
+ val = (*ctx % 0x7ffffffe) + 1;
+#endif
+ r = do_rand(&val);
+
+#ifdef USE_WEAK_SEEDING
+ *ctx = (unsigned int)val;
+#else
+ *ctx = (unsigned int)(val - 1);
+#endif
return (r);
}
-static u_long next = 1;
+static u_long next =
+#ifdef USE_WEAK_SEEDING
+ 1;
+#else
+ 2;
+#endif
int
rand()
@@ -104,6 +121,10 @@ srand(seed)
u_int seed;
{
next = seed;
+#ifndef USE_WEAK_SEEDING
+ /* Transform to [1, 0x7ffffffe] range. */
+ next = (next % 0x7ffffffe) + 1;
+#endif
}
@@ -125,6 +146,10 @@ sranddev()
mib[0] = CTL_KERN;
mib[1] = KERN_ARND;
sysctl(mib, 2, (void *)&next, &len, NULL, 0);
+#ifndef USE_WEAK_SEEDING
+ /* Transform to [1, 0x7ffffffe] range. */
+ next = (next % 0x7ffffffe) + 1;
+#endif
}