Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is RandomUtils.nextFloat in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

nextFloat is a statica method that belongs to a class rather than an instance of a class. method of the RandomUtils class that returns a random float value.

Syntax


public static float nextFloat()

Parameters

The nextFloat method doesn’t take any parameters.

Return value

This method returns a random float value.


The returned random float value will be within 0 to Float.MAX_VALUE.

Import RandomUtils

RandomUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.


<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.


Code

In the code below, we use the nextFloat method as follows.

import org.apache.commons.lang3.RandomUtils;
class NextFloatExample {
public static void main( String args[] ) {
System.out.println("RandomUtils.nextFloat() => " + RandomUtils.nextFloat()); // random float value
System.out.println("RandomUtils.nextFloat() => " + RandomUtils.nextFloat()); // random float value
}
}

Explanation

In the code above:

  • Line 1: We import the RandomUtils class.

import org.apache.commons.lang3.RandomUtils;

  • Lines 5 and 6: We use the nextFloat method to get the random float value.

  • Below is the output.


RandomUtils.nextFloat(); // random float value

RandomUtils.nextFloat(); //random float value

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring