What is RandomUtils.nextDouble in Java?
nextDouble is a static method of the RandomUtils class that will return a random double 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-langpackage, refer to the Maven Repository.
Syntax
public static double nextDouble()
Parameter
The nextDouble method does not take a parameter.
Return value
This method returns a random double value.
The returned random double value will be within the range of
0toDouble.MAX_VALUE.
Code
In the code below, we use the nextDouble method.
import org.apache.commons.lang3.RandomUtils;class NextDoubleExample {public static void main( String args[] ) {System.out.println("RandomUtils.nextDouble() => " + RandomUtils.nextDouble()); // random double valueSystem.out.println("RandomUtils.nextDouble() => " + RandomUtils.nextDouble()); // random double value}}
Explanation
In the code above:
- In line 1, we imported the
RandomUtilsclass.
import org.apache.commons.lang3.RandomUtils;
- In lines 5 and 6, we used the
nextDoublemethod to get the random double value.
Expected output
103
500
The output above may vary whenever the code is run. This is because every time, a random number is going to be generated.