Java Math.random()有什么功能呢?
下文笔者将讲述Math.random()方法的功能简介说明,如下所示:
Math.random()方法的功能:
用于返回一个位于[0,1)的随机数
Math.random语法:
static double random()
参数说明:
默认可不输入参数信息
返回值
返回double值
例
public class TestClass{
public static void main(String args[]){
System.out.println( Math.random() );
System.out.println( Math.random() );
}
}
例2:
生0-100 的随机数
import java.util.Random;
public class TestClass{
public static void main(String[] args){
Random rand=new Random();
int i=(int)(Math.random()*100); // 生成0-100的随机数
int j=rand.nextInt(100); // 这里是一个方法的重载,参数的内容是指定范围
System.out.println("i:"+i+"\nj:"+j); // 分别输出两个随机数
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


