java Random doubles() Method



Description

The java Random doubles() method returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive). A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Declaration

Following is the declaration for java.util.Random.doubles() method.

public DoubleStream doubles()

Parameters

NA

Return Value

The method call returns a stream of pseudorandom double values.

Exception

NA

java Random doubles​(double randomNumberOrigin, double randomNumberBound) Method

Description

The java Random doubles​(double randomNumberOrigin, double randomNumberBound) method returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Declaration

Following is the declaration for java.util.Random.doubles​(double randomNumberOrigin, double randomNumberBound) method.

public DoubleStream doubles​(double randomNumberOrigin, double randomNumberBound)

Parameters

randomNumberOrigin − the origin (inclusive) of each random value

randomNumberBound − the bound (exclusive) of each random value

Return Value

The method call a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive).

Exception

IllegalArgumentException − if randomNumberOrigin is greater than or equal to randomNumberBound

java Random doubles​(long streamSize) Method

Description

The java Random doubles​(long streamSize) method returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive). A pseudorandom double value is generated as if it's the result of calling the method nextDouble().

Declaration

Following is the declaration for java.util.Random.doubles​(long streamSize) method.

public DoubleStream doubles​(long streamSize)

Parameters

long streamSize − the number of values to generate.

Return Value

The method call a stream of double values.

Exception

IllegalArgumentException − if streamSize is less than zero.

java Random doubles​(long streamSize, double randomNumberOrigin, double randomNumberBound) Method

Description

The java Random doubles​(long streamSize, double randomNumberOrigin, double randomNumberBound) method returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

Declaration

Following is the declaration for java.util.Random.doubles​(long streamSize, double randomNumberOrigin, double randomNumberBound) method.

public DoubleStream doubles​(long streamSize, double randomNumberOrigin, double randomNumberBound)

Parameters

long streamSize − the number of values to generate.

Return Value

The method call a stream of double values.

Exception

IllegalArgumentException − if streamSize is less than zero.

Getting a Random Double Number Example

The following example shows the usage of Java Random doubles() method. Firstly, we've created a Random object and then using doubles() we retrieved a stream of random double values and printed first value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles();
      // print a random double value
      randomNoStream.limit(1).forEach( i -> System.out.println(i));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

0.5129590222446587

Getting a Random Double Number Between Given Two Numbers Example

The following example shows the usage of Java Random doubles(double randomNumberOrigin, double randomNumberBound) method. Firstly, we've created a Random object and then using doubles(double randomNumberOrigin, double randomNumberBound) we retrieved a stream of random double values and printed first value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles(5,6);
      // print a random double value
      randomNoStream.limit(1).forEach( i -> System.out.println(i));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

5.948299414215264

Getting a Stream of Random Double Numbers of Given Size Example

The following example shows the usage of Java Random doubles(long streamSize) method. Firstly, we've created a Random object and then using doubles(long streamSize) we retrieved a stream of a random double value and printed its value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles(1);
      // print a random double value
      randomNoStream.forEach( i -> System.out.println(i));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

0.716435513160095

Getting a Stream of Random Double Numbers of Given Size Between Given Two Numbers Example

The following example shows the usage of Java Random doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) method. Firstly, we've created a Random object and then using doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) we retrieved a stream of a random double value and printed its value.

package com.tutorialspoint;

import java.util.Random;
import java.util.stream.DoubleStream;

public class RandomDemo {
   public static void main( String args[] ) {

      // create random object
      DoubleStream randomNoStream = new Random().doubles(1,5,6);
      // print a random double value
      randomNoStream.forEach( i -> System.out.println(i));
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

5.980651774805819
java_util_random.htm
Advertisements