当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Random doubles()用法及代码示例


  • 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() 方法返回:

  1. 包含伪随机双精度值的流。
  2. 包含伪随机双精度值的流,每个值都有一个来源和界限。
  3. 包含双值的流。
  4. 包含伪随机双精度值的流,每个值都有一个来源和界限。

抛出

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 doubles() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。