Java Random - longs() Method
The java.util.Random.longs() method returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
Parameters
streamSize |
Specify the number of values to generate. |
randomNumberOrigin |
Specify the origin (inclusive) of each random value. |
randomNumberBound |
Specify the bound (exclusive) of each random value. |
Return Value
Returns a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive).
Exception
- Throws IllegalArgumentException, if streamSize is less than zero.
- Throws IllegalArgumentException, if randomNumberOrigin is greater than or equal to randomNumberBound.
Example:
In the example below, the java.util.Random.longs() method is used to get a stream of pseudorandom long values in the given range.
import java.util.*; import java.util.stream.LongStream; public class MyClass { public static void main(String[] args) { //creating a random object Random rand = new Random(); //generating a stream containing 10 long random //numbers between 500(inclusive) and 1000(exclusive) LongStream stream = rand.longs(10, 500, 1000); //printing all random numbers from the stream stream.forEach(System.out::println); } }
One of the possible outcome is given below:
740 558 989 934 852 937 525 503 646 980
❮ Java.util - Random