The manual of drand48()
says:
The drand48() and erand48() functions return non-negative, double-precision, floating-point values, uniformly distributed over the interval [0.0 , 1.0].
(source)
So just for clarification: Does this mean that 1.0 can be returned by drand48()
? Or does it mean that the return value will be < 1.0?
I'm asking because in a brute force test drand48()
never seems to return 1.0. It often returns 0, but never 1.0. However, for me the manual of drand48()
seems to say that 1.0 is indeed a result that could come up, although I haven't been able to make drand48()
return 1.0 in practice.
Anybody here who can shed some light onto this? Will the result always be less than 1.0 or can it also be equal to 1.0?
"over the interval [0.0 , 1.0]." It is safe to assume mathematical notation of interval. Parenthesis interval ends are exclusive and square brackets interval ends are inclusive. So according to the notation the returned value can be 1.0
.
However this is a documentation of an old version and the current ones specify [0.0, 1.0)
as the interval (which means it can't be 1.0
):
The Single UNIX ® Specification, Version 2 http://pubs.opengroup.org/onlinepubs/7908799/xsh/drand48.html
lists [0.0, 1.0]
while
The Open Group Base Specifications Issue 7, 2018 edition
http://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html
Linux Man Pages - POSIX.1-2001
https://www.systutorials.com/docs/linux/man/3-drand48/
Linux man page - POSIX.1-2001.
https://linux.die.net/man/3/erand48
all list [0.0, 1.0)
So I would be inclined to believe [0.0, 1.0)
is the correct specification. (it could be that the definition changed since SUS2 which is from 1997 or that it is a typo on the page, but I have no expertise on this so I won't weigh in)
in a brute force test drand48() never seems to return 1.0. It often returns 0, but never 1.0.
This seems highly suspicious. 0.0
should be have the same probability to appear as any other number in the interval so "It often returns 0" doesn't look right. Check that you print with full precision.
As a side note be aware that dran48 and friends are obsolete