Java中的 StrictMath random() 方法
random()是Java中 StrictMath 类的内置方法,用于获取大于或等于 0.0 且小于 1.0 的正号双精度值。 random() 方法被准确地组织以默认多个线程的适当使用。返回的值是伪随机采用的,具有该范围内的恒定分布。
句法:
public static double random()
参数:该方法不接受任何参数。
返回值:该方法返回大于等于 0.0 且小于 1.0 的伪随机双精度数。
下面的程序说明了Java.lang.StrictMath.random() 方法:
方案一:
java
// Java program to illustrate the
// Java.lang.StrictMath.random() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
double random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
}
}
java
// Java program to illustrate the
// Java.lang.StrictMath.random() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
double random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
}
}
输出:
Generated random number = 0.7276560829796844
Generated random number = 0.6646167632286143
方案二:
Java
// Java program to illustrate the
// Java.lang.StrictMath.random() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args) {
double random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
random_num = StrictMath.random();
System.out.println("Generated random number = "+ random_num);
}
}
输出:
Generated random number = 0.5071995313935024
Generated random number = 0.6938224427158157