Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

1. java.util.stream.DoublePipeline$Head@14ae5a5
2. java.util.stream.DoublePipeline$Head@7f31245a
3. java.util.stream.DoublePipeline$Head@6d6f6e28
4. java.util.stream.DoublePipeline$Head@135fbaa4

Example 2

Test it Now

Output:

1. java.util.stream.DoublePipeline$Head@14ae5a5

Example 3

Test it Now

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

Test it Now

Output:

1. java.util.stream.DoublePipeline$Head@14ae5a5
2. java.util.stream.DoublePipeline$Head@7f31245a
3. java.util.stream.DoublePipeline$Head@6d6f6e28

Example 5

Test it Now

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.

Next TopicJava Random




Help Others, Please Share

facebook twitter pinterest