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

Edit

Example:


2. Generate random float in range with Random class

Edit

Example:


3. Generate random float in range with Random and DoubleStream

Edit

Example:


4. Generate random float in range with Math

Edit

Example:


5. Test if float in range methods work as expected

Edit

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.

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


References

Edit

ThreadLocalRandom - Java docs
Math - Java docs
Random - Java docs
DoubleStream - Java docs

1
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