From owner-freebsd-current Sun Feb 2 4:30:44 2003 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E244C37B406 for ; Sun, 2 Feb 2003 04:30:37 -0800 (PST) Received: from nagual.pp.ru (pobrecita.freebsd.ru [194.87.13.42]) by mx1.FreeBSD.org (Postfix) with ESMTP id CE4B843F43 for ; Sun, 2 Feb 2003 04:30:36 -0800 (PST) (envelope-from ache@pobrecita.freebsd.ru) Received: from pobrecita.freebsd.ru (ache@localhost [127.0.0.1]) by nagual.pp.ru (8.12.6/8.12.6) with ESMTP id h12CUZd6063040; Sun, 2 Feb 2003 15:30:35 +0300 (MSK) (envelope-from ache@pobrecita.freebsd.ru) Received: (from ache@localhost) by pobrecita.freebsd.ru (8.12.6/8.12.6/Submit) id h12CUZe2063039; Sun, 2 Feb 2003 15:30:35 +0300 (MSK) (envelope-from ache) Date: Sun, 2 Feb 2003 15:30:35 +0300 From: "Andrey A. Chernov" To: Kris Kennaway Cc: current@FreeBSD.ORG Subject: Re: rand() is broken Message-ID: <20030202123035.GB62977@nagual.pp.ru> References: <20030202070644.GA9987@rot13.obsecurity.org> <20030202090422.GA59750@nagual.pp.ru> <20030202091106.GA72723@rot13.obsecurity.org> <20030202102621.GA60900@nagual.pp.ru> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-md5; protocol="application/pgp-signature"; boundary="+HP7ph2BbKc20aGI" Content-Disposition: inline In-Reply-To: <20030202102621.GA60900@nagual.pp.ru> User-Agent: Mutt/1.5.1i Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG --+HP7ph2BbKc20aGI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Feb 02, 2003 at 13:26:21 +0300, Andrey A. Chernov wrote: > Workaround I find so far is something like that >=20 > #define MASK 123459876 I found nothing better. Here is fix for 0 problem I plan to commit: --- stdlib/rand.c.old Sat Jan 4 20:39:19 2003 +++ stdlib/rand.c Sun Feb 2 14:43:42 2003 @@ -70,14 +70,18 @@ * Park and Miller, Communications of the ACM, vol. 31, no. 10, * October 1988, p. 1195. */ +#define SHIFT_MASK 123459876 long hi, lo, x; =20 - hi =3D *ctx / 127773; - lo =3D *ctx % 127773; + /* Can't be initialized with 0, so use shifting mask. */ + x =3D *ctx ^ SHIFT_MASK; + hi =3D x / 127773; + lo =3D x % 127773; x =3D 16807 * lo - 2836 * hi; - if (x <=3D 0) + if (x < 0) x +=3D 0x7fffffff; - return ((*ctx =3D x) % ((u_long)RAND_MAX + 1)); + *ctx =3D x ^ SHIFT_MASK; + return (x % ((u_long)RAND_MAX + 1)); #endif /* !USE_WEAK_SEEDING */ } =20 @@ -86,8 +90,10 @@ rand_r(unsigned int *ctx) { u_long val =3D (u_long) *ctx; - *ctx =3D do_rand(&val); - return (int) *ctx; + int r =3D do_rand(&val); + + *ctx =3D (unsigned int) val; + return (r); } =20 =20 --- stdlib/random.c.old Sun Mar 24 23:42:48 2002 +++ stdlib/random.c Sun Feb 2 15:24:38 2003 @@ -142,6 +142,10 @@ */ #define MAX_TYPES 5 /* max number of types above */ =20 +#ifndef USE_WEAK_SEEDING +#define SHIFT_MASK 123459876 +#endif + static long degrees[MAX_TYPES] =3D { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; static long seps [MAX_TYPES] =3D { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; =20 @@ -171,12 +175,12 @@ 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 0x27fb47b9, #else /* !USE_WEAK_SEEDING */ - 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, - 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, - 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, - 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, - 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, - 0xf3bec5da + 0x52a59789, 0x43164b1c, 0x7be52a82, 0x748ef343, 0x642a8923, 0x6ade1cd8, + 0x1ae76e27, 0x24b915ee, 0x2c42f326, 0x12ab3ee1, 0x4679af03, 0x876d19a0, + 0xe9e535ba, 0xad2471c8, 0x710262f8, 0xe1c16494, 0x29224bcc, 0x9710c348, + 0x7347f8e4, 0xe01ef1b4, 0x2030c33f, 0xd465e38, 0x925375aa, 0x6091d15d, + 0x467ee7d7, 0x92713312, 0x32346127, 0x8350e834, 0x3dadc6ea, 0x391364f2, + 0x8226561c, #endif /* !USE_WEAK_SEEDING */ }; =20 @@ -236,12 +240,14 @@ */ long hi, lo; =20 + /* Can't be initialized with 0, so use shifting mask. */ + x ^=3D SHIFT_MASK; hi =3D x / 127773; lo =3D x % 127773; x =3D 16807 * lo - 2836 * hi; - if (x <=3D 0) + if (x < 0) x +=3D 0x7fffffff; - return (x); + return (x ^ SHIFT_MASK); /* return state, not value */ #endif /* !USE_WEAK_SEEDING */ } =20 @@ -473,7 +479,12 @@ =20 if (rand_type =3D=3D TYPE_0) { i =3D state[0]; - state[0] =3D i =3D (good_rand(i)) & 0x7fffffff; + state[0] =3D good_rand(i); +#ifndef USE_WEAK_SEEDING + i =3D state[0] ^ SHIFT_MASK; +#else + i =3D state[0] & 0x7fffffff; +#endif } else { /* * Use local variables rather than static variables for speed. --=20 Andrey A. Chernov http://ache.pp.ru/ --+HP7ph2BbKc20aGI Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.0 (FreeBSD) iQCVAwUBPj0Pa+JgpPLZnQjrAQHMKQQAnZ6EThQ43Ds/Y9vWya76ZtOOrhizBTSj dMHZ2apqog6yiOvnh4EHBPq63keSJIBFQGXMihWk/Otw9JVJXgt9RiqoaYELRDPX mUAnVTNyGmF3/nR34u3B9Jiuaq7afA2gqglq4T3wvSU9o5ulbRVHLHgm7Vy25tEf kBSkWpBtJtE= =imhb -----END PGP SIGNATURE----- --+HP7ph2BbKc20aGI-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message