Generate Random Float type number in Java


In order to generate Random float type numbers in Java, we use the nextFloat() method of the java.util.Random class. This returns the next random float value between 0.0 (inclusive) and 1.0 (exclusive) from the random generator sequence.

Declaration −The java.util.Random.nextFloat() method is declared as follows −

public float nextFloat()

Let us see a program to generate random float type numbers in Java.

Example

 Live Demo

import java.util.Random;
public class Example {
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object
      System.out.println(rd.nextFloat()); // displaying a random float value between 0.0 and 1.0
   }
}

Output

0.7739767

Note − The output might vary on Online Compilers.

Updated on: 29-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements