随机数 (Random) 通过种子的伪随机。
线性求余算法
如果参数不填,默认种子是时间
下面代码中有Math的一些常用方法
1 public class Demo01 {
2
3 public static void main(String[] args) {
4
5
6 Random random=new Random();
7 int s=random.nextInt(10);
8 System.out.println(s);
9
10 //求绝对值
11 System.out.println(Math.abs(-2));
12
13 //浮点型保留2位小数
14 System.out.println(String.format("%.2f", Math.PI));
15
16 double a=-2.3;
17 //向上取整 没有有四舍五入
18 System.out.println(Math.ceil(a));
19 //向下取整 没有有四舍五入
20 System.out.println(Math.floor(a));
21 //最近取整,有四舍五入
22 System.out.println(Math.round(a));
23
24 //求幂 返回double类型
25 System.out.println(Math.pow(3, 4));
26
27 //求立方根
28 System.out.println(Math.cbrt(27));
29 //求平方根
30 System.out.println(Math.sqrt(25));
31
32 //求最大值
33 System.out.println(Math.max(23, 45));
34 //求最小值
35 System.out.println(Math.min(23, 45));
36
37 System.out.println(Math.nextAfter(5.1, 7.2));
38
39
40 }
41
42 }