Reputation: 43
I would like to know what the default seed for drand48()
is, i.e. if I don't explicitly use srand48()
before calling drand48()
?
I checked the source code of glibc 2.23 (the version I am currently using) and I think it is zero, but if someone can verify it, that would be nice.
Upvotes: 2
Views: 1533
Reputation: 78903
There is no specific value imposed by POSIX and in the contrary it says that one of the initialization functions should be called:
The
srand48()
,seed48()
, andlcong48()
functions are initialization entry points, one of which should be invoked before eitherdrand48()
,lrand48()
, ormrand48()
is called. (Although it is not recommended practice, constant default initializer values shall be supplied automatically ifdrand48()
,lrand48()
, ormrand48()
is called without a prior call to an initialization entry point.)
More generally, using functions that use a global shared state is not such a good idea. This set of functions has alternatives that received their state as function arguments.
If you don't call any of theses initialization functions, you go with the phrase in parenthesis. It basically says that the internal states should be initialized with defaults. This is not equivalent to call srand48
with a specific parameter because that function sets the low order bits to the fixed value 0x330
. It could be equivalent to a call to seed48(0, 0, 0)
, but this is not completely clear. The term constant default initializer values could mean that each implementation provides its default values, or that C's default initializers (all bit 0
) should be used.
Upvotes: 3