Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 1 Feb 2003 23:06:50 -0800
From:      Kris Kennaway <kris@obsecurity.org>
To:        current@FreeBSD.org, ache@FreeBSD.org
Subject:   rand() is broken
Message-ID:  <20030202070644.GA9987@rot13.obsecurity.org>

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

--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 <stdlib.h>
#include <stdio.h>

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




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