This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: random number generators - rand(), random(), etc


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]