Java ThreadLocalRandom nextDouble() method

The nextDouble() method of Java ThreadLocalRandom class returns a pseudorandom double value between 0 and 1. This method overrides the nextDouble in class Random.

Syntax:

Parameter:

NA

Returns:

This method returns a pseudorandom double value between 0 and 1.

Example

Output:

Random double value is: 0.2967936086945462

Java ThreadLocalRandom nextDouble(double n) method

The nextDouble(double n) method of Java ThreadLocalRandom class returns a pseudorandom. It returns the uniformly distributed double value between 0 and the specified value.

Syntax:

Parameter:

n: 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

Output:

Random double value is: 172.58603834580222

Example 2

Output:

Random double value is: 3961.37003622182

Example 3

Output:

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

Java ThreadLocalRandom nextDouble(double least, double bound) method

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

Syntax:

Parameter:

least - It is the least value.

bound - It is the upper bound (exclusive).

Returns:

This method returns the next value.

Exception:

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

Example 1

Output:

Random double value is: 5240.122245986785

Example 2

Output:

Random double value is: 17190.013525593356

Example 3

Output:

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