diff options
author | Renato Botelho <renato@netgate.com> | 2016-06-06 08:05:51 -0300 |
---|---|---|
committer | Renato Botelho <renato@netgate.com> | 2016-06-06 08:05:51 -0300 |
commit | 47dfb8d658406ebf07225c0104ebe4be06ae405f (patch) | |
tree | 83174cbd9592560c48ad2cd11afe42c5c12b6c1b /lib/libc/stdlib/rand.c | |
parent | 131cd15b13bbd3e141e911a65cf7a1895ec6ab05 (diff) | |
parent | 13d657a35d96e65f1be391830f36e1adff33534f (diff) | |
download | FreeBSD-src-47dfb8d658406ebf07225c0104ebe4be06ae405f.zip FreeBSD-src-47dfb8d658406ebf07225c0104ebe4be06ae405f.tar.gz |
Merge remote-tracking branch 'origin/stable/10' into devel
Diffstat (limited to 'lib/libc/stdlib/rand.c')
-rw-r--r-- | lib/libc/stdlib/rand.c | 57 |
1 files changed, 13 insertions, 44 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index 4f4aa8d..6125b1a 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -49,14 +49,6 @@ __FBSDID("$FreeBSD$"); static int do_rand(unsigned long *ctx) { -#ifdef USE_WEAK_SEEDING -/* - * Historic implementation compatibility. - * The random sequences do not vary much with the seed, - * even with overflowing. - */ - return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)RAND_MAX + 1)); -#else /* !USE_WEAK_SEEDING */ /* * Compute x = (7^5 * x) mod (2^31 - 1) * without overflowing 31 bits: @@ -67,64 +59,45 @@ do_rand(unsigned long *ctx) */ long hi, lo, x; - /* Must be in [1, 0x7ffffffe] range at this point. */ - hi = *ctx / 127773; - lo = *ctx % 127773; + /* Transform to [1, 0x7ffffffe] range. */ + x = (*ctx % 0x7ffffffe) + 1; + hi = x / 127773; + lo = x % 127773; x = 16807 * lo - 2836 * hi; if (x < 0) x += 0x7fffffff; - *ctx = x; /* Transform to [0, 0x7ffffffd] range. */ - return (x - 1); -#endif /* !USE_WEAK_SEEDING */ + x--; + *ctx = x; + return (x); } int -rand_r(unsigned int *ctx) +rand_r(unsigned *ctx) { u_long val; int r; -#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 + *ctx = (unsigned)val; return (r); } -static u_long next = -#ifdef USE_WEAK_SEEDING - 1; -#else - 2; -#endif +static u_long next = 1; int -rand() +rand(void) { return (do_rand(&next)); } void -srand(seed) -u_int seed; +srand(unsigned seed) { next = seed; -#ifndef USE_WEAK_SEEDING - /* Transform to [1, 0x7ffffffe] range. */ - next = (next % 0x7ffffffe) + 1; -#endif } @@ -136,7 +109,7 @@ u_int seed; * data from the kernel. */ void -sranddev() +sranddev(void) { int mib[2]; size_t len; @@ -146,10 +119,6 @@ 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 } |