Languages
[Edit]
EN

Java - 4 different ways to generate random float in range

4 points
Created by:
Erica33
439

1. Generate random float in range with ThreadLocalRandom

public static float nextFloatBetween(float min, float max) {
    return (ThreadLocalRandom.current().nextFloat() * (max - min)) + min;
}

Example:

System.out.println(nextFloatBetween(4.0f, 8.0f));     // 4.2842164
System.out.println(nextFloatBetween(100.0f, 900.0f)); // 457.32108
System.out.println(nextFloatBetween(-6.5f, -3.5f));   // -4.848301


2. Generate random float in range with Random class

public static float nextFloatBetween2(float min, float max) {
    return (new Random().nextFloat() * (max - min)) + min;
}

Example:

System.out.println(nextFloatBetween2(4.0f, 8.0f));     // 6.454987
System.out.println(nextFloatBetween2(100.0f, 900.0f)); // 518.71204
System.out.println(nextFloatBetween2(-6.5f, -3.5f));   // -3.6078863


3. Generate random float in range with Random and DoubleStream

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

Example:

System.out.println(nextFloatBetween3(4.0f, 8.0f));     // 4.026776
System.out.println(nextFloatBetween3(100.0f, 900.0f)); // 542.0356
System.out.println(nextFloatBetween3(-6.5f, -3.5f));   // -5.739404


4. Generate random float in range with Math

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

Example:

System.out.println(nextFloatBetween4(4.0f, 8.0f));     // 6.454987
System.out.println(nextFloatBetween4(100.0f, 900.0f)); // 518.71204
System.out.println(nextFloatBetween4(-6.5f, -3.5f));   // -3.6078863


5. Test if float in range methods work as expected

Generate 2000 float numbers in range 2.0f and 4.0f
Sort them in asc order.
Print all 2000 floats in sorted order and verify that all of the numbers at the beginning are around 2.0f and at the end around of 4.0f.

public static void testFloatInRange() {
    List<Float> list = new ArrayList<>();
    for (int i = 0; i < 2000; i++) {
        //float rand = nextFloatBetween(2.0f, 4.0f);
        //float rand = nextFloatBetween2(2.0f, 4.0f);
        //float rand = nextFloatBetween3(2.0f, 4.0f);
        float rand = nextFloatBetween4(2.0f, 4.0f);
        list.add(rand);
    }
    list.sort(Float::compareTo);

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

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

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

2.0001996
2.0011134
2.0021777
2.006852
2.0091434
...
3.9970121
3.9975452
3.9978209
3.9982586
3.9999447


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