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

Attachment: msg51490/pgp00000.pgp
Description: PGP signature

Reply via email to