java Random nextFloat() Method



Description

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

Declaration

Following is the declaration for java.util.Random.nextFloat() method.

public float nextFloat()

Parameters

NA

Return Value

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

Exception

NA

Getting a Random Float Value Example

The following example shows the usage of Java Random nextFloat() method. Firstly, we've created a Random object and then using nextFloat() we retrieved a random float value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random();
    
      // check next float value  
      System.out.println("Next float value: " + randomNo.nextFloat());
   }      
}

Output

Let us compile and run the above program, this will produce the following result.

Next float value: 0.63595074

Getting a Random Float Value with Given Seed Example

The following example shows the usage of Java Random nextFloat() method. Firstly, we've created a Random object with a given seed value and then using nextFloat() we retrieved a random float value and printed it.

package com.tutorialspoint;

import java.util.Random;

public class RandomDemo {
   public static void main( String args[] ) {
      
      // create random object
      Random randomNo = new Random(10);
    
      // check next float value  
      System.out.println("Next float value: " + randomNo.nextFloat());
   }      
}

Output

Let us compile and run the above program, this will produce the following result.

Next float value: 0.73043025
java_util_random.htm
Advertisements