Java – Create Random Float Value

To create a random float value in Java, use Random.nextFloat() method. Create java.util.Random class object and call nextFloat() method on this object. nextFloat() returns a randomly generated float value between -1.0F and 1.0F.

In the following example, we will create a random float value using nextFloat() method.

Java Program

</>
Copy
import java.util.Random;

public class Example{ 
    public static void main(String[] args) {
    	Random random = new Random();
    	float f = random.nextFloat();
    	System.out.println("Next random float value is : " + f);
    }
}

Output

Next random float value is : 0.9188673

Reference to Random.nextFloat() method for syntax and more example programs.

Conclusion

In this Java Tutorial, we learned how to create a random float value using java.util.Random.nextFloat() method.