Javatpoint Logo
Javatpoint Logo

Java ThreadLocalRandom nextLong() method

The nextLong() method of Java ThreadLocalRandom class returns a pseudorandom long value. This method overrides the nextLong in class Random.

Syntax:

Parameter:

NA

Returns:

This method returns a pseudorandom long value.

Example

Test it Now

Output:

Random long value is: 8925561344935968546

Java ThreadLocalRandom nextLong(long bound) method

The nextLong(long bound) method of Java ThreadLocalRandom class returns a pseudorandom. It returns the uniformly distributed value between 0 and bound.

Syntax:

Parameter:

bound: It is the bound on the random number. It must be positive.

Returns:

This method returns the next value.

Exception:

IllegalArgumentException - This exception will throw if n is not positive.

Example 1

Test it Now

Output:

Random long value is: 1703

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
	at java.base/java.util.concurrent.ThreadLocalRandom.nextInt(Unknown Source)
	at tests.JavaNextLongExample2.main(ThreadLocalRandomNextLongExample2.java:7)

Java ThreadLocalRandom nextLong(long origin, long bound) method

The nextLong(long origin, long bound) method of Java ThreadLocalRandom class returns a pseudorandom. It returns the uniformly distributed value between the given least value and bound.

Syntax:

Parameter:

origin - It is the least value.

bound - It is the upper bound (exclusive).

Returns:

This method returns a pseudorandom long value between the origin and the bound.

Exception:

IllegalArgumentException - This exception will throw if origin is greater than or equal to bound.

Example 1

Test it Now

Output:

Random long value is: 39171

Example 2

Test it Now

Output:

Random long value is: 42248

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
	at java.base/java.util.concurrent.ThreadLocalRandom.nextLong(Unknown Source)
	at tests.JavaNextLongExample2.main(ThreadLocalRandomNextLongExample3.java:7)






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA