How to generate random floating point number in Java
In this tutorial, we use the java.util.Random() class to generate the random floating-point numbers by using the nextFloat() method.
Click here, to learn more about the java.util.Random() class with nextFloat() method.
Code with Explanation
import java.util.Random; public class RandomFloatNumbers { public static void main(String[] args) { Random randomObj = new Random(); System.out.println("The generated Random Floating Number is "); System.out.println(randomObj.nextFloat()); } }
Output The generated Random Floating Number is 0.23058897
- Line number 5, here we create an object of Random class named as randomObj.
- Line number 7, using the randomObj we call the nextFloat() method which returns the random floating-point number.
This is enough for a quick glimpse into random floating number generation in Java. If you need to learn more about the Random class then you can click the given link above the code.
Leave a Reply