To get a random double number, instantiate a Random object and call the method nextDouble which returns a pseudo-random float between 0 and 1.
Main.java:
import java.util.*;
public class Main {
static Random r = new Random();
public static void main(String[] args) throws Exception {
for (int i=0; i<10; i++)
System.out.println(getRandomDouble(-10, 15));
}
public static double getRandomDouble(double min, double max) {
return min + (r.nextDouble() * (max - min));
}
}
Generating a random double between x and y
Joris Van den BogaertYou can use the formula:
To get a random double number, instantiate a Random object and call the method nextDouble which returns a pseudo-random float between 0 and 1.
Main.java:
outputs: