The option I'd suggest would be to use double
rather than float
.
The only disadvantages I've ever found generally only come into play when you have a large number of them, in that the storage requirements are higher, and this doesn't appear to be the case here.
If you must use float
, then the solution you've already tried is probably the best one available. It's a fundamental problem that loss of precision when converting double
to float
may give you 1.0f
.
So coerce the float value to be in your desired range.
However, you don't have to pepper your code with if
statements for this, simply provide a float random function that does the grunt work for you:
float frandom() {
float ret = 1.0f;
while (ret == 1.0f)
ret = (float) Math.random();
return ret;
}
or, as per your sample:
float frandom() {
float ret = (float) Math.random();
if (ret == 1.0f)
ret = 0.0f;
return ret;
}
then call it with:
float rand = frandom();
This would be the point where it would be nice for Java (and others) to have the Javascript ability to "inject" code into a type so that you could implement frandom()
into the static Math
arena. That way you could just use:
float rand = Math.frandom();
But, alas, this is not yet possible (as far as I know, short of recompiling the Java support libraries, which seems a bit of an overkill).
By the way, if you're looking for the maximum float value less than 1, it's 0.99999994
(an exponent multiplier of 2-1
with all mantissa bits set to 1
), so you could use that instead of 0.0f
in the frandom()
function above.
This is gleaned from a handy little tool I wrote some time ago, one that's proven invaluable when fiddling about with IEEE754 floating point values.