Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

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

The nextLong static method of the RandomUtils class returns a random long value.

Syntax


public static long nextLong()

Parameters

The nextLong method doesn’t take any parameters.

Return value

This method returns a random long value.


The returned random long value will be within 0 to Long.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 nextLong method as follows.

import org.apache.commons.lang3.RandomUtils;
class NextLongExample {
public static void main( String args[] ) {
System.out.println("RandomUtils.nextLong() => " + RandomUtils.nextLong()); // random long value
System.out.println("RandomUtils.nextLong() => " + RandomUtils.nextLong()); // random long 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 nextLong method to get the random long value.

  • Below is the output.


RandomUtils.nextLong(); // random long value

RandomUtils.nextLong(); //random long 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