import java.util.Random;
public class UsingMath {
public static void main(String [] args){
Random random = new Random();//new一个random对象
System.out.println("b 的随机数不为0 时为 " + random.nextBoolean());//随机布尔数
System.out.println("i1 的随机数为" + random.nextInt(100));//0-100之间的随机数
System.out.println("i2 的随机数为" + random.nextInt(100));//0-100之间的随机数
System.out.println("d1 的随机数为" + Math.random());//[0,1)之间的浮点数
System.out.println("d2 的随机数为" + Math.random());//[0,1)之间的浮点数
System.out.println("30 弧度的正弦值:Math.sin(Math.toRadians(30.0))=" + Math.sin(Math.toRadians(30.0)));//30 弧度的正弦值
System.out.println("E 的对数值:Math.log(Math.E)=" + Math.log(Math.E));//E 的对数值
System.out.println("2 的3 次方:Math.pow(2.0, 3.0)=" + Math.pow(2.0, 3.0));//2 的3 次方
System.out.println("33.6F 四舍五入:Math.round(33.6F)=" + Math.round(33.6F));//33.6F 四舍五入
}
}
运行结果:
Math.random()的值默认为[0,1)之间的随机数。
依靠这个特点可以做出理论上任何随机区间。