summaryrefslogtreecommitdiffstats
path: root/lib/libc
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2003-02-04 11:24:08 +0000
committerache <ache@FreeBSD.org>2003-02-04 11:24:08 +0000
commit406a1bc7bbfd2c7d241946c23ede8ec59fd436e2 (patch)
treeb794978ed6263b8a5298e3cbb4e07dc4fce01682 /lib/libc
parent150cf2a7fd2d4c9336894a5581fa9dee511a6f57 (diff)
downloadFreeBSD-src-406a1bc7bbfd2c7d241946c23ede8ec59fd436e2.zip
FreeBSD-src-406a1bc7bbfd2c7d241946c23ede8ec59fd436e2.tar.gz
For rand(3) and random(3) TYPE_0 drop NSHUFF values right after srand{om}()
to remove part of seed -> 1st value correlation. Correlation still remains because of algorithm limits. Note that old algorithm have even stronger correlation, especially in the lower bits area, but not eye-visible, as current one.
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdlib/rand.c8
-rw-r--r--lib/libc/stdlib/random.c13
2 files changed, 15 insertions, 6 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 47bb36d..dcc445f 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -51,6 +51,8 @@ __FBSDID("$FreeBSD$");
#include <stdio.h>
#endif /* TEST */
+#define NSHUFF 100 /* to drop part of seed -> 1st value correlation */
+
static int
do_rand(unsigned long *ctx)
{
@@ -108,7 +110,11 @@ void
srand(seed)
u_int seed;
{
+ int i;
+
next = seed;
+ for (i = 0; i < NSHUFF; i++)
+ (void)do_rand(&next);
}
@@ -137,7 +143,7 @@ sranddev()
unsigned long junk;
gettimeofday(&tv, NULL);
- next = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk;
+ srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk);
}
}
diff --git a/lib/libc/stdlib/random.c b/lib/libc/stdlib/random.c
index 82af94c..64310cd 100644
--- a/lib/libc/stdlib/random.c
+++ b/lib/libc/stdlib/random.c
@@ -142,6 +142,8 @@ __FBSDID("$FreeBSD$");
*/
#define MAX_TYPES 5 /* max number of types above */
+#define NSHUFF 100 /* to drop part of seed -> 1st value correlation */
+
static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
@@ -264,19 +266,20 @@ void
srandom(x)
unsigned long x;
{
- long i;
+ long i, lim;
+ state[0] = x;
if (rand_type == TYPE_0)
- state[0] = x;
+ lim = NSHUFF;
else {
- state[0] = x;
for (i = 1; i < rand_deg; i++)
state[i] = good_rand(state[i - 1]);
fptr = &state[rand_sep];
rptr = &state[0];
- for (i = 0; i < 10 * rand_deg; i++)
- (void)random();
+ lim = 10 * rand_deg;
}
+ for (i = 0; i < lim; i++)
+ (void)random();
}
/*
OpenPOWER on IntegriCloud