> My co-worker was troubleshooting why some of our unittests (that work on > multiple operating systems and architectures) failed on OpenBSD and saw > that if you call srandom(0) to initialize the RNG, random() will always > return 0. (I was able to reproduce this.) > > If this is expected behaviour, please point to explanation. > > He posted his tests and discussed the code related to this at: > > https://banu.com/blog/42/openbsd-bug-in-the-random-function/ > > He suggests two ways to fix this: > > 1) if (x == 0) > x = 1; > > upon entry in srandom(). That's the glibc way and he thinks is the > better solution as it falls back to the default behavior (when srandom > is not called) when the impossible seed=0 is passed. > > 2) the second way to fix it is the FreeBSD way: > > for (i = 1; i < rand_deg; i++) { > + if (state[i-1] == 0) > + state[i-1] = 123459876; > val = div(state[i-1], 127773);
Already fixed. CVSROOT: /cvs Module name: src Changes by: [email protected] 2012/03/21 06:36:49 Modified files: lib/libc/stdlib: random.c Log message: Fix a bug where random() always returns 0 when srandom() is seeded with 0. Use 1 and not 0 as the first element of the state array, similar to what glibc does. OK nicm@
