Languages
[Edit]
EN

Java - 4 different ways to generate random double in range

10 points
Created by:
Xitiz
2195

1. Generate random double in range with ThreadLocalRandom

public static double nextDoubleBetween(double min, double max) {
    return (ThreadLocalRandom.current().nextDouble() * (max - min)) + min;
}

Example:

System.out.println(nextDoubleBetween(4.0d, 8.0d));     // 7.413037434364464
System.out.println(nextDoubleBetween(100.0d, 900.0d)); // 847.7762445206072
System.out.println(nextDoubleBetween(-6.5d, -3.5d));   // -4.858339745847818


2. Generate random double in range with Random class

public static double nextDoubleBetween2(double min, double max) {
    return (new Random().nextDouble() * (max - min)) + min;
}

Example:

System.out.println(nextDoubleBetween2(4.0d, 8.0d));     // 4.052304029464722
System.out.println(nextDoubleBetween2(100.0d, 900.0d)); // 768.620491027832
System.out.println(nextDoubleBetween2(-6.5d, -3.5d));   // -5.824402153491974


3. Generate random double in range with Random and DoubleStream

public static double nextDoubleBetween3(double min, double max) {
    // java 8 + DoubleStream
    return new Random().doubles(min, max).limit(1).findFirst().getAsDouble();
}

Example:

System.out.println(nextDoubleBetween3(4.0d, 8.0d));     // 5.957144260406494
System.out.println(nextDoubleBetween3(100.0d, 900.0d)); // 859.1829833984375
System.out.println(nextDoubleBetween3(-6.5d, -3.5d));   // -4.081723213195801


4. Generate random double in range with Math

public static double nextDoubleBetween4(double min, double max) {
    return (Math.random() * (max - min)) + min;
}

Example:

System.out.println(nextDoubleBetween4(4.0d, 8.0d));     // 5.137720584869385
System.out.println(nextDoubleBetween4(100.0d, 900.0d)); // 603.9947204589844
System.out.println(nextDoubleBetween4(-6.5d, -3.5d));   // -4.018362045288086


5. Test if double in range methods work as expected

public static void testDoubleInRange() {
	List<Double> list = new ArrayList<>();
	for (int i = 0; i < 2000; i++) {
		double rand = nextDoubleBetween(2.0d, 4.0d);
		//double rand = nextDoubleBetween2(2.0d, 4.0d);
		//double rand = nextDoubleBetween3(2.0d, 4.0d);
		//double rand = nextDoubleBetween4(2.0d, 4.0d);
		list.add(rand);
	}
	list.sort(Double::compareTo);

	// print 2000 sorted doubles in asc order
	list.forEach(System.out::println);
}

public static void main(String[] args) {
	testDoubleInRange();
}

Output (only first 5 x doubles and last 5 x doubles):

2.0010556305772016
2.0012970579895804
2.001744530227831
2.002021244118838
2.003799609313888
...
3.9969095941710693
3.9974610558327024
3.9980601986911837
3.998751363307557
3.999256829257631


References

ThreadLocalRandom - Java docs
Math - Java docs
Random - Java docs
DoubleStream - Java docs

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - random numbers

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join