Java Randomlongs Examples
In this tutorial, we will learn about the Java Random.longs() method, and learn how to use this method to generate a stream of long type numbers, with the help of examples.
longs
Random.longs() returns an effectively unlimited stream of pseudorandom long values.
Syntax
The syntax of longs() method is
Random.longs()
Returns
The method returns LongStream object.
Example 1 longs
In this example, we will generate an unlimited sequence of random long type numbers using longs() method and print out four of them to the console.
Java Program
import java.util.Random; import java.util.function.LongConsumer; import java.util.stream.LongStream; public class Example { public static void main(String[] args) { Random random = new Random(); LongStream ds = random.longs(); ds.limit(4).forEach(new LongConsumer() { @Override public void accept(long value) { System.out.println(value); } }); } }
Output
-6251239855392854015 -7974780473092051021 -2652163404078734145 7738474359912325515
longslong streamSize
Random.longs() returns a stream producing the given streamSize number of pseudorandom long values.
Syntax
The syntax of longs() method with stream size is
Random.longs(long streamSize)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the stream. |
Returns
The method returns LongStream object.
Example 2 longsstreamSize
In this example, we will generate an specific number of long values, say eight, using longs(streamSize) method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random; import java.util.function.LongConsumer; import java.util.stream.LongStream; public class Example { public static void main(String[] args) { long streamSize = 8; Random random = new Random(); LongStream ds = random.longs(streamSize); ds.forEach(new LongConsumer() { @Override public void accept(long value) { System.out.println(value); } }); } }
Output
-6830441378515779608 -1179248027058137291 -5606058920192108368 8178120771225726595 -3472126381625498085 7782182188885249485 -4504231265999262239 -4107022727140179889
longslong randomNumberOrigin, long randomNumberBound
Random.longs() returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of longs() method with random number origin and random number bound is
Random.longs(long randomNumberOrigin, long randomNumberBound)
where
Parameter | Description |
---|---|
randomNumberOrigin | The origin (inclusive) of each random value. |
randomNumberBound | The bound (exclusive) of each random value. |
Returns
The method returns LongStream object.
Example 3 longsrandomNumberOrigin, randomNumberBound
In this example, we will generate an unlimited sequence of random long values, which are limited by an origin and bound using longs() method and print out four of these numbers from the stream to the console.
Java Program
import java.util.Random; import java.util.function.LongConsumer; import java.util.stream.LongStream; public class Example { public static void main(String[] args) { long randomNumberOrigin = 154; long randomNumberBound = 200; Random random = new Random(); LongStream ds = random.longs(randomNumberOrigin, randomNumberBound); ds.limit(4).forEach(new LongConsumer() { @Override public void accept(long value) { System.out.println(value); } }); } }
Output
188 174 194 188
longslong streamSize, long randomNumberOrigin, long randomNumberBound
Random.longs() returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).
Syntax
The syntax of longs() method with stream size, random number origin and random number bound is
Random.longs(long streamSize, long randomNumberOrigin, long randomNumberBound)
where
Parameter | Description |
---|---|
streamSize | The number of values to generate in the Stream. |
randomNumberOrigin | The origin (inclusive) of each random value in the Stream. |
randomNumberBound | The bound (exclusive) of each random value in the Stream. |
Returns
The method returns LongStream object.
Example 4 longs streamSize, randomNumberOrigin, randomNumberBound
In this example, we will generate eight random numbers of type long which are limited by an origin and bound using longs() method and print out these random numbers from the stream to the console.
Java Program
import java.util.Random; import java.util.function.LongConsumer; import java.util.stream.LongStream; public class Example { public static void main(String[] args) { long streamSize = 8; long randomNumberOrigin = 154; long randomNumberBound = 200; Random random = new Random(); LongStream ds = random.longs(streamSize, randomNumberOrigin, randomNumberBound); ds.forEach(new LongConsumer() { @Override public void accept(long value) { System.out.println(value); } }); } }
Output
192 172 155 170 179 157 188 197
Conclusion
In this Java Tutorial, we have learnt the syntax of Java Random.longs() method, and also learnt how to use this method with the help of examples.