Java Random doubles() Method

  • The doubles() method of Random class returns a stream of pseudorandom double values, each conforming between zero and one.
  • The second syntax effectively returns an unlimited stream of pseudorandom double values, each conforming to the given randomNumberOrigin and randomNumberBound.
  • The third syntax returns a stream producing the pseudorandom double values for the given streamSize number, each between zero and one.
  • The last syntax returns a stream producing the given streamSizenumber of pseudorandom double values, each conforming to the given origin and bound.

Syntax

Parameter

streamSize- the number of values to generate

randomNumberOrigin- the origin of each random value

randomNumberBound- the bound of each random value

Return Value

The doubles() method returns :

  1. a stream containing pseudorandom double values.
  2. a stream containing pseudorandom double values, each having an origin and bound.
  3. a stream containing double values.
  4. a stream containing pseudorandom double values, each having an origin and bound.

Throws

IllegalArgumentException: if streamSize is a negative value.

IllegalArgumentException: if randomNumberOrigin is greater than or equal to randomNumberBound.

Example 1

Output:

Example 2

Output:

Example 3

Output:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be greater than origin
	atjava.util.Random.doubles(Random.java:983)
	at com.javaTpoint.JavaRandomDoublesExample3.main(JavaRandomDoublesExample3.java:14)

If randonNumberOrigin is greater than or equal to randomNumberBound, it will always give you the above-described runtime error, i.e., IllegalArgumentException.

Example 4

Output:

Example 5

Output:

Exception in thread "main" java.lang.IllegalArgumentException: size must be non-negative
	atjava.util.Random.doubles(Random.java:887)
	at com.javaTpoint.JavaRandomDoublesExample5.main(JavaRandomDoublesExample5.java:14)

If streamSize is less than zero, it will give you IllegalArgumentException, as described above.