I just found out the hard way that srand(1)
resets the PRNG of C(++) to the state before any call to srand
(as defined in the reference).
However, the seed 0 seems to do the same, or the state before any call to srand
seems to use the seed 0.
What’s the difference between those two calls or what is the reason they do the same thing?
For example this code (execute on Ideone)
#include <stdio.h>
#include <stdlib.h>
int main() {
for (int seed = 0; seed < 4; seed++ ) {
printf( "Seed %d:", seed);
srand( seed );
for(int i = 0; i < 5; i++ )
printf( " %10d", rand() );
printf( "\n");
}
return 0;
}
returns
Seed 0: 1804289383 846930886 1681692777 1714636915 1957747793
Seed 1: 1804289383 846930886 1681692777 1714636915 1957747793
Seed 2: 1505335290 1738766719 190686788 260874575 747983061
Seed 3: 1205554746 483147985 844158168 953350440 612121425
How glibc does it:
around line 181 of glibc/stdlib/random_r.c, inside function
__srandom_r
/* We must make sure the seed is not 0. Take arbitrarily 1 in this case. */ if (seed == 0) seed = 1;
But that's just how glibc does it. It depends on the implementation of the C standard library.