Java Random nextFloat() Method
Example
Get a random float number, and check that it is at least 0.0 and less than 1.0:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
float num = rand.nextFloat();
System.out.println("Is the number at least 0.0 and less than 1.0? " + (num >= 0.0f && num < 1.0f));
}
}
Definition and Usage
The nextFloat() method returns a random float value from 0.0 up to 1.0. It can return 0.0, but never 1.0.
A float stores fewer digits than a double. In most programs, nextDouble() is a better choice.
Syntax
public float nextFloat()
Parameter Values
None.
Technical Details
| Returns: | A random float value, at least 0.0 and less than 1.0. |
|---|
Related Pages
Java How To Generate Random Numbers
❮ Random Methods