I've been using java's SplittableRandom ever since I heard about it, due to its speed, and not being in need of multithreading. However, although it has almost every method from Random class, it doesn't come with nextFloat()
. Why's that?
Now, the real question is, how would I then go about creating that nextFloat
method? Seeing the double
is generated as follows: (from JDK 8)
final double internalNextDouble(final double n, final double n2) {
double longBitsToDouble = (this.nextLong() >>> 11) * 1.1102230246251565E-16;
if (n < n2) {
longBitsToDouble = longBitsToDouble * (n2 - n) + n;
if (longBitsToDouble >= n2) {
longBitsToDouble = Double.longBitsToDouble(Double.doubleToLongBits(n2) - 1L);
}
}
return longBitsToDouble;
}
.. I was hoping that I could just turn it to a 32-bit number generation with the following;
final float internalNextFloat(final float min, final float max) {
float intBitsToFloat = (this.nextInt() >>> 11) * 1.1102230246251565E-16f;
if (min < max) {
intBitsToFloat = intBitsToFloat * (max - min) + min;
if (intBitsToFloat >= max) {
intBitsToFloat = Float.intBitsToFloat(Float.floatToIntBits(max) - 1);
}
}
return intBitsToFloat;
}
However, this returns 0.000000
. I can only assume it overflows somewhere, in which case I'm pretty sure the problem lies at the following line:
(this.nextInt() >>> 11) * 1.1102230246251565E-16f;
So, not being experienced with shifting (and using epsilon I guess), how could I achieve what I want?
Aucun commentaire:
Enregistrer un commentaire