This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: random number generators - rand(), random(), etc
- From: Eric Rannaud <e at nanocritical dot com>
- To: jb <jb dot 1234abcd at gmail dot com>, libc-alpha <libc-alpha at sourceware dot org>
- Date: Fri, 7 Nov 2014 10:35:19 -0800
- Subject: Re: random number generators - rand(), random(), etc
- Authentication-results: sourceware.org; auth=none
- References: <loom dot 20141107T185212-863 at post dot gmane dot org>
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.