Languages
[Edit]
EN

Java - get random float number

12 points
Created by:
JustMike
26120

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

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join