Java – Create Random Long Value
To create a random long value in Java, use Random.nextLong() method. Create java.util.Random class object and call nextLong() method on this object. nextLong() returns a randomly generated long value.
In the following example, we will create a random long value using nextLong() method.
Java Program
</>
Copy
import java.util.Random;
public class Example{
public static void main(String[] args) {
Random random = new Random();
long l = random.nextLong();
System.out.println("Next random long value is : " + l);
}
}
Output
Next random long value is : 5015315584966125576
Reference to Random.nextLong() method for syntax and more example programs.
Conclusion
In this Java Tutorial, we learned how to create a random long value using java.util.Random.nextLong() method.