Use the <
operator.
While this is a simplification, consider that computers store fractions by adding terms of the form 2−n. Most real numbers can't be represented exactly, and literals like 0.6
are converted to the nearest double
value from a finite set.
Likewise, the random()
function can't generate real values from a continuous range between zero and one. Instead, it chooses an integer from N elements in the range 0 to N − 1, then divides it by N to yield a (rational) result in the range [0, 1). If you want to satisfy a condition with a probability of P, it should be true for P ⋅ N elements from the set of N possibilties.
Since zero is counted as one of the elements, the maximum result value that should be included is ( P ⋅ N − 1 ) / N. Or, in other words, we should exclude P ⋅ N / N, i.e., P.
That exclusion of P is what leads to the use of <
.
It might be easier to reason about when you consider how you'd use a method like nextInt()
, There, the effect of zero on a small range is more obvious, and your expression would clearly use the <
operator: current().nextInt(5) < 3
. That wouldn't change if you divide the result: current().nextInt(5) / 5.0 < 0.6
. The difference between nextInt(5) / 5.0
and random()
is only that the latter has many more, much smaller steps between 0 and 1.
I apologize for misleading people with my original answer, and thank the user who straightened me out.