Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

randomutils
java
communitycreator

What is RandomUtils.nextDouble 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.

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-lang package, 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 0 to Double.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 value
System.out.println("RandomUtils.nextDouble() => " + RandomUtils.nextDouble()); // random double value
}
}

Explanation

In the code above:

  • In line 1, we imported the RandomUtils class.
import org.apache.commons.lang3.RandomUtils;
  • In lines 5 and 6, we used the nextDouble method 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.


RELATED TAGS

randomutils
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