I know that this question is more than a year old, but other answers here lack important points. The best way to generate a random floating point value in Java (assuming you don't need a specific seed and you don't need to be cryptographically secure ) is with the ThreadLocalRandom
class.
For example, if you need a method that generates random numbers between two other numbers, you can write it like this:
public static double generateRandomDouble(double min, double max) { return ThreadLocalRandom.current().nextDouble(min, max); }
- The problem with
Math.random
is that this leads to a conflict . In other words, if your code runs simultaneously with any other code that uses Math.random
, your random number may need to be counted. - The problem with
new Random()
is that it creates a new instance of Random
every time it is called, which is additional work and more verbose. - The problem with both of these methods is that they force you to do additional mathematical calculations to scale them to the desired range, making your code less readable and more error prone. The
ThreadLocalRandom
class has a very convenient nextDouble
overload which allows you to specify "origin" and "bound", that is, a minimum and a maximum. - Another problem with both of these methods is that they are inaccurate. As you do the scaling, you need to consider the rounding problems inherent in floating point arithmetic. In fact, all other answers to this question sometimes give answers equal to max .
In the above code, I make two assumptions. First, you want the bottom border to be inclusive, and the top border exclusive. This assumption is rather trivial, because the probability of an actual landing at the border is very small. This is also what other answers try to do (although sometimes they give answers equal to max, unlike mine). The second assumption I make is that by “random floating point value” you mean the floating point value that I accepted based on the fact that you marked the question with floating-point . So I gave a method that returns a double
value, which is a double-precision floating point value, and what you will usually use in Java for floating point values. However, if you wanted to specify a primitive with float
, you can use the following method:
public static float generateRandomFloat(float min, float max) { if (min >= max) throw new IllegalArgumentException("max must be greater than min"); float result = ThreadLocalRandom.current().nextFloat() * (max - min) + min; if (result >= max)
Unfortunately, overloading nextFloat
in ThreadLocalRandom
is possible because they expect very few people to need a random float
with a float
. To write this method correctly, you need to fix floating-point rounding errors. Therefore, I highly recommend that you use the version that produces double
.
In conclusion, you should never use Math.random
and should not create a new instance of Random
every time you need to generate a random number (unless, of course, you need a specific starting number). When generating random numbers, the ThreadLocalRandom
class is used by default.