Home » Java ThreadLocalRandom longs() Method with Examples

Java ThreadLocalRandom longs() Method with Examples

by Online Tutorials Library

Java ThreadLocalRandom longs() method

The longs() method of Java ThreadLocalRandom class returns an effectively unlimited stream of pseudorandom long values. This method overrides longs in class Random.

Syntax:

Parameter:

NA

Returns:

This method returns a stream of pseudorandom long values.

Example

Test it Now

Output:

stream of pseudorandom long value is: [email protected]  

Java ThreadLocalRandom longs(long streamSize) method

The longs(long streamSize) method of Java ThreadLocalRandom class returns a stream producing the given streamSize number of pseudorandom long values. This method overrides longs in class Random.

Syntax:

Parameter:

streamSize – It is the number of values to generate

Returns:

This method returns a stream of long values.

Exception:

IllegalArgumentException: This exception will throw if the streamSize is less than zero.

Example 1

Test it Now

Output:

stream of longs value is: [email protected]  

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative  at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source)  at tests.ThreadLocalRandomLongsExample1.main(ThreadLocalRandomLongsExample2.java:7)  

Java ThreadLocalRandom longs(long randomNumberOrigin, long randomNumberBound) method

The longs(long randomNumberOrigin, long randomNumberBound) method of Java ThreadLocalRandom class returns an effectively unlimited stream of pseudorandom long values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides longs in class Random.

Syntax:

Parameter:

randomNumberOrigin: It is the origin (inclusive) of each random value.

randomNumberBound: It is the bound (exclusive) of each random value.

Returns:

This method returns a stream of pseudorandom long values.

Exception:

IllegalArgumentException: This exception will throw if randomNumberOrigin is greater than or equal to randomNumberBound.

Example 1

Test it Now

Output:

stream of pseudorandom long value is: [email protected]  

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be greater than origin  at java.base/java.util.concurrent.ThreadLocalRandom.longs(Unknown Source)  at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample2.java:7)  

Java ThreadLocalRandom longs(long streamSize, long randomNumberOrigin, long randomNumberBound) method

The longs(long streamSize, long randomNumberOrigin, long randomNumberBound) method of Java ThreadLocalRandom class returns a stream producing the given streamSize number of pseudorandom long values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides longs in class Random.

Syntax:

Parameter:

streamSize – It is the number of values to generate

randomNumberOrigin – It is the origin (inclusive) of each random value

randomNumberBound – It is the bound (exclusive) of each random value

Returns:

This method returns a stream of pseudorandom long values.

Exception:

  1. IllegalArgumentException: This exception will throw if the streamSize is less than zero.
  2. IllegalArgumentException: This exception will throw if the randomNumberOrigin is greater than or equal to randomNumberBound.

Example 1

Test it Now

Output:

stream of pseudorandom long value is: [email protected]  

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative  at java.base/java.util.concurrent.ThreadLocalRandom.doubles(Unknown Source)  at tests.ThreadLocalRandomLongsExample2.main(ThreadLocalRandomLongsExample1.java:7)  

You may also like