Java Utility Library

Java Random - nextFloat() Method



The java.util.Random.nextFloat() method returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

Syntax

public float nextFloat()

Parameters

No parameter is required.

Return Value

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

Exception

NA

Example:

In the example below, the java.util.Random.nextFloat() method is used to get pseudorandom number, uniformly distributed float value between 0.0 and 1.0.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a random object
    Random rand = new Random();

    //printing uniformly distributed float random numbers
    for(int i = 0; i < 5; i++)
      System.out.println("Next Float Value: " + rand.nextFloat()); 
  }
}

One of the possible outcome is given below:

Next Float Value: 0.008743644
Next Float Value: 0.7637772
Next Float Value: 0.20614451
Next Float Value: 0.9271508
Next Float Value: 0.59462535

❮ Java.util - Random