Home » Java programming language

Java Random nextFloat() Method with Example

Random Class nextFloat() method: Here, we are going to learn about the nextFloat() method of Random Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

Random Class nextFloat() method

  • nextFloat() method is available in java.util package.
  • nextFloat() method is used to generate the next pseudo-random float value between the range 0.0 and 1.0 from this Random Value Generator.
  • nextFloat() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • nextFloat() method does not throw an exception at the time of returning float.

Syntax:

    public float nextFloat();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is float, it returns next pseudo-random distributed float value between the given range from this Random Generator.

Example:

// Java program to demonstrate the example 
// of float nextFloat() method of 
// Random

import java.util.*;

public class NextFloatOfRandom {
 public static void main(String args[]) {
  // Instantiates Random object
  Random ran = new Random();

  // By using nextFloat() method is
  // to return float pseudo-random
  // value between 0.0 and 1.0 by
  // using Random Value Generator
  float val = ran.nextFloat();

  // Display 
  System.out.println("ran.nextFloat(): " + val);
 }
}

Output

RUN 1:
ran.nextFloat(): 0.89347947

RUN 2:
ran.nextFloat(): 0.48533785

RUN 3:
ran.nextFloat(): 0.2989604


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.