数学班静态双随机() (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