From owner-freebsd-current Sat Feb 1 23: 6:54 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 4B69B37B405; Sat, 1 Feb 2003 23:06:52 -0800 (PST) Received: from obsecurity.dyndns.org (adsl-67-115-74-80.dsl.lsan03.pacbell.net [67.115.74.80]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8737543F43; Sat, 1 Feb 2003 23:06:51 -0800 (PST) (envelope-from kris@obsecurity.org) Received: from rot13.obsecurity.org (rot13.obsecurity.org [10.0.0.5]) by obsecurity.dyndns.org (Postfix) with ESMTP id 23FF867C6F; Sat, 1 Feb 2003 23:06:51 -0800 (PST) Received: by rot13.obsecurity.org (Postfix, from userid 1000) id C0E6FE6C; Sat, 1 Feb 2003 23:06:50 -0800 (PST) Date: Sat, 1 Feb 2003 23:06:50 -0800 From: Kris Kennaway To: current@FreeBSD.org, ache@FreeBSD.org Subject: rand() is broken Message-ID: <20030202070644.GA9987@rot13.obsecurity.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="azLHFNyN32YCQGCU" Content-Disposition: inline User-Agent: Mutt/1.4i 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 --azLHFNyN32YCQGCU Content-Type: text/plain; charset=us-ascii Content-Disposition: inline FreeBSD's rand() implementation has been broken for the past 23 months, since the following commit: ---- Revision 1.3 / (download) - annotate - [select for diffs], Tue Feb 27 14:42:19 2001 UTC (23 months ago) by ache Branch: MAIN Changes since 1.2: +26 -0 lines Diff to previous 1.2 (colored) Use formula with better random distribution for rand() Even better formula from random() could not be intetgrated because rand_r() supposed to store its state in the single variable (but table needed for random() algorithm integration). ---- The following simple test program exhibits the breakage: #include #include int main() { int i; for(i=1; i<=1000; i++) { srand(i); printf("%d: %d\n", i, rand()); } } 1: 16807 2: 33614 3: 50421 4: 67228 5: 84035 6: 100842 7: 117649 8: 134456 9: 151263 10: 168070 11: 184877 12: 201684 13: 218491 14: 235298 15: 252105 16: 268912 17: 285719 18: 302526 ... i.e. the first value returned from rand() is correlated with the seed given to srand(). This is a big problem unless your seed is randomly chosen over its entire integer range. I noticed this because awk exhibits the same problem, and the script seeds the generator with a PID. The script works fine under 4.x since the rand() implementation does not have this "feature". Kris --azLHFNyN32YCQGCU Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (FreeBSD) iD8DBQE+PMOEWry0BWjoQKURAjt9AJ0asKgLSFpPOJ9IyWdFosIWBSLQGgCgg86B YF64B92l+R2/qDAKtvKrNCM= =A1cI -----END PGP SIGNATURE----- --azLHFNyN32YCQGCU-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message