Java Math类静态double random()示例

数学班静态双随机() (Math Class static double random() )

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This method is used to return a random positive double type number, which will be greater than or equal to 0.0 but less than 1.0.

    此方法用于返回一个随机的正双精度型数字,该数字将大于或等于0.0但小于1.0。

  • This is a static method, so it is accessible with the class name too.

    这是一个静态方法,因此也可以使用类名进行访问。

  • In this method, we get random values by the pseudorandom-number generator.

    在这种方法中,我们通过伪随机数生成器获得随机值。

  • This method generates one new pseudorandom-number, which is similar to when we write an expression as, Random r = new Random();

    此方法生成一个新的伪随机数,类似于我们将表达式编写为: Random r = new Random();。

  • This is a synchronized method that means this method is accessible by more than one thread, but one thread at a time.

    这是一种同步方法,这意味着该方法可被多个线程访问,但一次只能访问一个线程。

  • We should go for this method if we generate pseudorandom-number at a huge rate by more than one thread.

    如果我们通过多个线程以极大的速率生成伪随机数,则应该采用这种方法。

  • In this method, we don't pass any parameter.

    在这种方法中,我们不传递任何参数。

  • This method does not throw any exception.

    此方法不会引发任何异常。

Syntax:

句法:

    public static double random(){
    }

Parameter(s): void - it does not accpet any parameter.

参数(S): 空 -它不Accpet头的任何参数。

Return value:

返回值:

The return type of this method is doube, it returns a value greater than or equal to 0.0 but less than 1.0.

此方法的返回类型是doube ,它返回的值大于或等于0.0但小于1.0。

Java程序演示random()方法的示例 (Java program to demonstrate example of random() method)

// Java program to demonstrate the example of 
// random() method of Math Class.

public class RandomMethod {
    public static void main(String[] args) {
        // declaring variables & calling method
        double d1 = Math.random();
        double d2 = Math.random();
        double d3 = Math.random();

        // printing values
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
    }
}

Output

输出量

E:\Programs>javac RandomMethod.java

E:\Programs>java RandomMethod
d1: 0.5999311914345431
d2: 0.27793400080453723
d3: 0.38280263956313376


翻译自: https://www.includehelp.com/java/math-class-static-double-random-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值