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- 它是每個隨機值的界限(不包括)
返回值:
此方法返回偽隨機雙精度值流。
異常:
- IllegalArgumentException: 如果 streamSize 小於零,將拋出此異常。
- 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 longs()用法及代碼示例
- Java ThreadLocalRandom current()用法及代碼示例
- Java ThreadLocalRandom ints()用法及代碼示例
- Java ThreadLocalRandom nextLong()用法及代碼示例
- Java ThreadLocalRandom setSeed()用法及代碼示例
- Java ThreadLocalRandom nextDouble()用法及代碼示例
- Java ThreadLocalRandom nextGaussian()用法及代碼示例
- Java ThreadLocalRandom nextInt()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread destroy()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ThreadLocalRandom doubles() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。