From 406a1bc7bbfd2c7d241946c23ede8ec59fd436e2 Mon Sep 17 00:00:00 2001 From: ache Date: Tue, 4 Feb 2003 11:24:08 +0000 Subject: 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. --- lib/libc/stdlib/rand.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/libc/stdlib/rand.c') 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 #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); } } -- cgit v1.1