Re: Apache Commons RNG - nextDoubleOpen() for next double in the open (0,1) interval

Gilles Sadowski <[email protected]>
Newsgroups gmane.comp.jakarta.commons.devel
Message-ID <CACioL_GYKHC3AOd_FQ_HewV7r6u_+9K5ZFrAuzB8V0--nWN+sQ@mail.gmail.com>
Hello.

Le jeu. 12 févr. 2026 à 13:54, Jherek Healy
<[email protected]> a écrit :
>
> 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.

Is this the sole use-case?
If so, wouldn't it be better (design-wise) to implement the functionality
in the "o.a.c.rng.sampling.distribution" package?

Regards,
Gilles

[1] https://commons.apache.org/proper/commons-rng/commons-rng-sampling/index.html

> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.