Home » Java ThreadLocalRandom ints() Method with Examples

Java ThreadLocalRandom ints() Method with Examples

by Online Tutorials Library

Java ThreadLocalRandom ints() method

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

Syntax:

Parameter:

NA

Returns:

This method returns a stream of pseudorandom int values.

Example 1

Test it Now

Output:

stream of pseudorandom int value is: [email protected]  

Java ThreadLocalRandom ints(long streamSize) method

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

Syntax:

Parameter:

streamSize – It is the number of values to generate

Returns:

This method returns a stream of int values.

Exception:

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

Example 1

Test it Now

Output:

stream of int 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.ints(Unknown Source)  at tests.ThreadLocalRandomIntsExample2.main(ThreadLocalRandomIntsExample2.java:7)  

Java ThreadLocalRandom ints(int randomNumberOrigin, int randomNumberBound) method

The ints(int randomNumberOrigin, int randomNumberBound) method of Java ThreadLocalRandom class returns an effectively unlimited stream of pseudorandom int values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides ints 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 int 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 int 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.ints(Unknown Source)  at tests.ThreadLocalRandomIntsExample2.main(ThreadLocalRandomIntsExample2.java:7)  

Java ThreadLocalRandom ints(long streamSize, int randomNumberOrigin, int randomNumberBound) method

The ints(long streamSize, int randomNumberOrigin, int randomNumberBound) method of Java ThreadLocalRandom class returns a stream producing the given streamSize number of pseudorandom int values. Each value must conform to the given origin (inclusive) and bound (exclusive). This method overrides ints 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 int values.

Exception:

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

Example 1

Test it Now

Output:

stream of pseudorandom int 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.ints(Unknown Source)  at tests.ThreadLocalRandomIntsExample2.main(ThreadLocalRandomIntsExample2.java:7)  

You may also like