How to generate random decimal numbers in java within range?


I am trying to generate random numbers in Java within a certain range with the following code:

int randomNum = rand.nextInt((max - min) + 1) + min;

But this only generates numbers in the set of integers. How can I get numbers that are also decimals?


rand should have a nextDouble method. This method does not accept arguments since it generates a number between 0.0 and 1.0 To fit that number to your range you can use a code similar to this:

double randomNum = min + ( max - min ) * rand.nextDouble();
Scroll to Top