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: jb <jb dot 1234abcd at gmail dot com>
- To: libc-alpha at sourceware dot org
- Date: Fri, 7 Nov 2014 20:07:47 +0000 (UTC)
- 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> <CAH_=xoaB7zNpcTXu72nq1gjRYhJDsVjheMh5gOsYCwZgPHRAOQ at mail dot gmail dot com>
Eric Rannaud <e <at> nanocritical.com> writes:
> ...
> 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;
> }
> ...
Yes, indeed.
But, if they share the *single instance* of the generator, does it not make
both of them non-reentrant and non-threadsafe since they share a state (it
might be e.g. a seed value) that is modified on each call by one of them ?
jb