Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 Feb 2003 14:46:29 +0300
From:      "Andrey A. Chernov" <ache@nagual.pp.ru>
To:        Kris Kennaway <kris@obsecurity.org>
Cc:        current@FreeBSD.ORG
Subject:   Final fix for correlation problem (was Re: rand() is broken)
Message-ID:  <20030203114629.GA77557@nagual.pp.ru>
In-Reply-To: <20030202090422.GA59750@nagual.pp.ru>
References:  <20030202070644.GA9987@rot13.obsecurity.org> <20030202090422.GA59750@nagual.pp.ru>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
This is final fix for 1st value correlation problem. Somebody with 
math statistic knowledge please run some test to be sure that NSHUFF is 
good enough for this simple PRNG (i.e. not 100% good, but good for average 
quality PRNG we have). My simple test shown that 100 is enough, but I 
am not PRNG math expert. Maybe increasing NSHUFF up too 2000 required.

Similar patch is needed for random(3) TYPE_0 too, I'll produce it when we
find good NSHUFF.

--- stdlib/rand.c.bak	Mon Feb  3 13:22:12 2003
+++ stdlib/rand.c	Mon Feb  3 14:03:58 2003
@@ -51,6 +51,8 @@
 #include <stdio.h>
 #endif /* TEST */
 
+#define NSHUFF 100      /* to drop seed -> 1st value correlation */
+
 static int
 do_rand(unsigned long *ctx)
 {
@@ -108,7 +110,11 @@
 srand(seed)
 u_int seed;
 {
+	int i;
+
 	next = seed;
+	for (i = 0; i < NSHUFF; i++)
+		(void)do_rand(&next);
 }
 
 
@@ -137,7 +143,7 @@
 		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);
 	}
 }
 

-- 
Andrey A. Chernov
http://ache.pp.ru/

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (FreeBSD)

iQCVAwUBPj5WleJgpPLZnQjrAQHqRgQAiH7mWI2K30s/m9dilOVcj5v62kVcPoW5
rADaGKbs44d3r6XVyFnlB32I4HNq74n8LhGQmxZTe0dHAF4Q4P3n0mLk4GgKyNEq
nrAfJ0IE6y7YuEa71uACYq2G0DRPRITve/T80kWsDoTAHFA/Z/LG7OR2Q83UmrXz
l6FSYPzmLk8=
=juZn
-----END PGP SIGNATURE-----

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030203114629.GA77557>