random number generators - rand(), random(), etc
Eric Rannaud
e@nanocritical.com
Fri Nov 7 18:35:00 GMT 2014
On November 7, 2014 10:16:47 AM PST, jb <jb.1234abcd@gmail.com> wrote:
>--- srandom(s), random()
>seed random numbers
>none 1804289383 846930886
>--- srand(s), rand()
>seed random numbers
>none 844158168 953350440
It appears rand() and random() not only share the implementation, they
also share a single *instance* of the random number generator. These
two programs give the same output:
int main() {
fprintf(stderr, "%ld\n", random());
fprintf(stderr, "%d\n", rand());
return 0;
}
int main() {
fprintf(stderr, "%d\n", rand());
fprintf(stderr, "%ld\n", random());
return 0;
}
The reason why your test produces a different output for rand() with
'none' seed, is that by that point you already have called random().
Indeed, you print for rand() with 'none': 844158168, which is the 3rd
value produced by random() when seeded with 3.
Maybe the manpage for rand(3) could be made a little clearer.
More information about the Libc-alpha
mailing list