- Random 類的 doubles() 方法返回偽隨機雙精度值流,每個值都在 0 和 1 之間。
- 第二種語法有效地返回無限的偽隨機雙精度值流,每個值都符合給定的 randomNumberOrigin 和 randomNumberBound。
- 第三個語法返回一個流,為給定的 streamSize 數字生成偽隨機雙精度值,每個值都在 0 和 1 之間。
- 最後一個語法返回一個流,產生給定的 streamSizenumber 偽隨機雙精度值,每個值都符合給定的原點和邊界。
用法
public DoubleStream doubles()
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
public DoubleStream doubles(long streamSize)
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound)
參數
streamSize- 要生成的值的數量
randomNumberOrigin- 每個隨機值的來源
randomNumberBound- 每個隨機值的界限
返回值
doubles() 方法返回:
- 包含偽隨機雙精度值的流。
- 包含偽隨機雙精度值的流,每個值都有一個來源和界限。
- 包含雙值的流。
- 包含偽隨機雙精度值的流,每個值都有一個來源和界限。
拋出
IllegalArgumentException: 如果 streamSize 是負值。
IllegalArgumentException:如果 randomNumberOrigin 大於或等於 randomNumberBound。
例子1
import java.util.Random;
public class JavaRandomDoublesExample1 {
public static void main(String[] args) {
//create random object
Random random= new Random();
// will return a pseudorandom double value btw zero and one
for (int i=1;i<5;i++) {
System.out.println(i+". "+random.doubles());
}
}
}
輸出:
例子2
import java.util.Random;
import java.util.stream.DoubleStream;
public class JavaRandomDoublesExample2 {
static int i=1;
public static void main(String[] args) {
double randomNumberOrigin = 1;
double randomNumberBound = 7;
Random random = new Random();
/*returns a stream of pseudorandom double value
conforming to the given origin and bound */
DoubleStream d1 = random.doubles(randomNumberOrigin, randomNumberBound);
System.out.println(i + ". " + d1);
}
}
輸出:
例子3
import java.util.Random;
import java.util.stream.DoubleStream;
public class JavaRandomDoublesExample3 {
public static void main(String[] args) {
double randomNumberOrigin=1;
//bound must be greater than origin
double randomNumberBound=1;
Random random=new Random();
/*returns a stream of pseudorandom double value
conforming to the given origin and bound */
DoubleStream d1=random.doubles(randomNumberOrigin,randomNumberBound);
System.out.println(d1);
}
}
輸出:
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)
如果 randonNumberOrigin 大於或等於 randomNumberBound,它將始終為您提供 above-described 運行時錯誤,即 IllegalArgumentException。
示例 4
import java.util.Random;
import java.util.stream.DoubleStream;
public class JavaRandomDoublesExample4 {
static int i=1;
public static void main(String[] args) {
double randomNumberOrigin=1;
double randomNumberBound=7;
long streamSize=12358;
Random random=new Random();
DoubleStream d1=random.doubles(randomNumberOrigin,randomNumberBound);
/*returns a stream producing given streamsize number
of pseudorandom double value*/
DoubleStream d2 =random.doubles(streamSize);
/*returns a stream producing given streamsize number
of pseudorandom double value, each between the given origin
and bound*/
DoubleStream d3 =random.doubles(streamSize,randomNumberOrigin,randomNumberBound);
System.out.println(i+". "+d1);
System.out.println(i+1 +". "+d2);
System.out.println(i+2+". "+d3);
}
}
輸出:
例 5
import java.util.Random;
import java.util.stream.DoubleStream;
public class JavaRandomDoublesExample5 {
public static void main(String[] args) {
double randomNumberOrigin=1;
double randomNumberBound=12;
//size must be non-negative
long streamSize=-1;
Random random=new Random();
DoubleStream d1=random.doubles(randomNumberOrigin,randomNumberBound);
DoubleStream d2 =random.doubles(streamSize);
DoubleStream d3 =random.doubles(streamSize,randomNumberOrigin,randomNumberBound);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(Math.random());
}
}
輸出:
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)
如果 streamSize 小於零,它會給你 IllegalArgumentException,如上所述。
相關用法
- Java Random setSeed()用法及代碼示例
- Java Random nextDouble()用法及代碼示例
- Java Random nextInt()用法及代碼示例
- Java Random nextGaussian()用法及代碼示例
- Java Random nextBoolean()用法及代碼示例
- Java Random nextBytes()用法及代碼示例
- Java Random nextFloat()用法及代碼示例
- Java Random next()用法及代碼示例
- Java Random nextLong()用法及代碼示例
- Java RandomAccessFile close()用法及代碼示例
- Java RandomAccessFile readByte()用法及代碼示例
- Java RandomAccessFile readChar()用法及代碼示例
- Java RandomAccessFile writeUTF()用法及代碼示例
- Java RandomAccessFile readUnsignedByte()用法及代碼示例
- Java RandomAccessFile writeFloat()用法及代碼示例
- Java RandomAccessFile getFD()用法及代碼示例
- Java RandomAccessFile writeChar()用法及代碼示例
- Java RandomAccessFile writeBytes()用法及代碼示例
- Java RandomAccessFile readInt()用法及代碼示例
- Java RandomAccessFile readShort()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Random doubles() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。