Java random()方法用于生成介于0.0
和1.0
之间的随机数。 范围是:0.0 =< Math.random() < 1.0
。 通过使用算术运算可以实现不同的范围。
语法
static double random()
参数
- 这是一个默认方法,不接受任何参数。
返回值
- 此方法返回一个
double
值。
示例
public class Test {
public static void main(String args[]) {
System.out.println( Math.random() );
System.out.println( Math.random() );
}
}
执行上面示例代码,得到以下结果:
0.16763945061451657
0.400551253762343