Apache Commons RNG - nextDoubleOpen() for next double in the open (0,1) interval
Jherek Healy <[email protected]>
| Newsgroups | gmane.comp.jakarta.commons.devel |
|---|---|
| Message-ID | <a5xWRzIucrVkOEvPY20n5VSIxyMT7H2inQ2CyhuSVMUb16XpFBmxwkCvMgg7WKx3Xt63W4djrcB6HcKWDMtXJ6TdA-2HRhaCGWlFlFMpHOE=@protonmail.com> |
Dear Commons RNG Team,
I am proposing to introduce a new method in IntProvider and UniformRandomProvider which computes a random double number in the open interval (0, 1).
Right now, nextDouble() computes a random double in the semi-closed interval [0,1). This can be problematic when the random number is to be used in a inverse distribution function, to provide random numbers according to a specific distribution, as the inverse distribution function is only defined on the open interval.
The idea is to match the implementation of https://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/VERSIONS/C-LANG/mt19937-64.cgenrand64_real3
double genrand64_real3(void)
{
return ((genrand64_int64() >> 12) + 0.5) * (1.0/4503599627370496.0);
}
There are two possible Java implementations:
((nextLong() >>> 12) + 0.5) * 0x1.0p-52;
or equivalently (reusing the constant used for the semi-closed interval)
((v >>> 11) | 1) * * 0x1.0p-53;
Yet another alternvative (which produces different numbers (last digit)) is the union trick:
long bits = (random64 >>> 12) | 0x3FF0000000000001L;
return Double.longBitsToDouble(bits) - 1.0;
I don't have a strong preference in either of the choices.
Jherek