Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - 4 different ways to generate random float in range

0 contributions
4 points

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

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more