Languages
[Edit]
EN

Java - get random float number

12 points
Created by:
Flimzy
596

1. Generate random float with ThreadLocalRandom class

public static float nextFloat() {
    return ThreadLocalRandom.current().nextFloat();
}

Example:

System.out.println(nextFloat()); // 0.5261965
System.out.println(nextFloat()); // 0.23931652
System.out.println(nextFloat()); // 0.8099988


2. Generate random float with Random class

public static float nextFloat2() {
    return new Random().nextFloat();
}

Example:

System.out.println(nextFloat2()); // 0.0030902028
System.out.println(nextFloat2()); // 0.77623594
System.out.println(nextFloat2()); // 0.39141566


References

ThreadLocalRandom - Java docs
Random - 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