Java Random nextLong() Method
Example
Get a random long number:
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand = new Random();
long num = rand.nextLong();
System.out.println(num);
}
}
Note: The output is different each time you run the program. The number can be positive or negative.
Definition and Usage
The nextLong() method returns a random long value. The number can be positive or negative.
A long can hold much larger numbers than an int. Use this method when you need a large random number, for example a random ID number.
Tip: To get a random number in a range, use nextInt() with a bound. In Java 8, nextLong() has no version with a bound.
Syntax
public long nextLong()
Parameter Values
None.
Technical Details
| Returns: | A random long value. |
|---|
Related Pages
Java How To Generate Random Numbers
❮ Random Methods