當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java ThreadLocalRandom doubles()用法及代碼示例

Java ThreadLocalRandom 類的 doubles() 方法返回一個有效無限的偽隨機雙精度值流。每個值必須介於 0 和 1 之間。此方法覆蓋 Random 類中的雙精度值。

用法:

public DoubleStream doubles()

參數:

NA

返回值:

此方法返回偽隨機雙精度值流。

示例

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample1 {
    public static void main(String[] args) {
    	
    	// Returns a stream of pseudorandom double values   	
    	System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles());
    }
}

輸出:

stream of pseudorandom double value is:[email protected]

Java ThreadLocalRandom doubles(long streamSize) 方法

Java ThreadLocalRandom 類的 doubles(long streamSize) 方法返回一個流,該流產生給定的 streamSize 數量的偽隨機雙精度值。每個值必須符合給定的 0(含)和 1(不含)。此方法覆蓋 Random 類中的雙精度數。

用法:

public DoubleStream doubles(long streamSize)

參數:

streamSize- 它是要生成的值的數量

返回值:

此方法返回雙精度值流。

異常:

IllegalArgumentException:如果 streamSize 小於零,將拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample2 {
    public static void main(String[] args) {
    	
    	// Returns a stream of double values   	
    	System.out.println("stream of double value is:" +ThreadLocalRandom.current().doubles(100));
    }
}

輸出:

stream of double value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample3 {
    public static void main(String[] args) {
    	
    	// Returns exception because streamSize is negative 
    	System.out.println("stream of double value is:" +ThreadLocalRandom.current().doubles(-20));
    }
}

輸出:

Exception in thread "main" java.lang.IllegalArgumentException:size must be non-negative
	at java.base/java.util.concurrent.ThreadLocalRandom.doubles(Unknown Source)
	at tests.JavaNextIntExample1.main(ThreadLocalRandomDoublesExample2.java:8)

Java ThreadLocalRandom doubles(double randomNumberOrigin, double randomNumberBound) 方法

Java ThreadLocalRandom 類的 doubles(double randomNumberOrigin, double randomNumberBound) 方法返回偽隨機雙精度值的有效無限流。每個值必須符合給定的原點(包含)和邊界(不包含)。此方法覆蓋 Random 類中的雙精度數。

用法:

public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)

參數:

randomNumberOrigin:它是每個隨機值的原點(含)。

randomNumberBound:它是每個隨機值的界限(不包括)。

返回值:

此方法返回偽隨機雙精度值流。

異常:

IllegalArgumentException:如果 randomNumberOrigin 大於或等於 randomNumberBound,則會拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample4 {
    public static void main(String[] args) {
    	
    	// Returns a stream of pseudorandom double values   	
    	System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles(20, 30));
    }
}

輸出:

stream of pseudorandom double value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample5 {
    public static void main(String[] args) {
    	
    	// Returns exception because origin is greater than bound  	
    	System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles(30, 10));
    }
}

輸出:

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

Java ThreadLocalRandom doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) 方法

Java ThreadLocalRandom 類的 doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) 方法返回一個流,該流產生給定的 streamSize 數量的偽隨機雙精度值。每個值必須符合給定的原點(包含)和邊界(不包含)。此方法覆蓋 Random 類中的雙精度數。

用法:

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

參數:

streamSize- 它是要生成的值的數量

randomNumberOrigin- 是每個隨機值的原點(含)

randomNumberBound- 它是每個隨機值的界限(不包括)

返回值:

此方法返回偽隨機雙精度值流。

異常:

  1. IllegalArgumentException: 如果 streamSize 小於零,將拋出此異常。
  2. IllegalArgumentException: 如果 randomNumberOrigin 大於或等於 randomNumberBound,則會拋出此異常。

例子1

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample6 {
    public static void main(String[] args) {
    	
    	// Returns a stream of pseudorandom double values   	
    	System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles(10, 20, 30));
    }
}

輸出:

stream of pseudorandom double value is:[email protected]

例子2

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomDoublesExample7 {
    public static void main(String[] args) {
    	
    	// Returns exception because steamSize is negative   	
    	System.out.println("stream of pseudorandom double value is:" +ThreadLocalRandom.current().doubles(-10, 20, 30));
    }
}

輸出:

Exception in thread "main" java.lang.IllegalArgumentException:size must be non-negative
	at java.base/java.util.concurrent.ThreadLocalRandom.doubles(Unknown Source)
	at tests.JavaNextIntExample1.main(ThreadLocalRandomDoublesExample2.java:7)






相關用法


注:本文由純淨天空篩選整理自 Java ThreadLocalRandom doubles() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。