Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

Java - get random float number

0 contributions
12 points

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

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