Math类:
无构造方法
import java.util.Random;
public class MathDemo {
public static void main(String[] args) {
double d1 = Math.ceil(12.34);//大于参数的最小整数
double d2 = Math.floor(12.34);//小于参数的最大整数
double d3 = Math.round(12.34);//四舍五入
System.out.println("d1="+d1);
System.out.println("d2="+d2);
System.out.println("d3="+d3);
// System.out.println(Math.pow(10, 3));//10的3次方
// System.out.println(Math.sqrt(2));//开根号
/*for(int x = 0; x<10;x++){
int d = (int)Math.ceil(Math.random()*6);
System.out.println(d);
}*/
Random r = new Random();
// double dd = r.nextDouble();
int dd = r.nextInt(6)+1;//随机1-6
System.out.println("Random中的dd="+dd);
}
}
import java.util.Random;
public class MathDemo {
public static void main(String[] args) {
double d1 = Math.ceil(12.34);//大于参数的最小整数
double d2 = Math.floor(12.34);//小于参数的最大整数
double d3 = Math.round(12.34);//四舍五入
System.out.println("d1="+d1);
System.out.println("d2="+d2);
System.out.println("d3="+d3);
// System.out.println(Math.pow(10, 3));//10的3次方
// System.out.println(Math.sqrt(2));//开根号
/*for(int x = 0; x<10;x++){
int d = (int)Math.ceil(Math.random()*6);
System.out.println(d);
}*/
Random r = new Random();
// double dd = r.nextDouble();
int dd = r.nextInt(6)+1;//随机1-6
System.out.println("Random中的dd="+dd);
}
}
Random类:
有构造方法
Random类中的nextDouble()方法和Math类中的random()方法效果是一样的。但是Math类中的方法返回值大多是double类型的,如果需要int类型的结果还需要做强转。Random类中的方法就比较便捷,可以直接获取int类型值,改变随机种子也比较方便。
关注我的微信公众号(曲健磊的个人随笔),观看更多精彩内容: