zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package util;
 
import java.math.BigDecimal;
import java.util.Random;
 
public class RandomUtil {
 
    public static final String numberChar = "0123456789";
 
    public static int random(int min, int max) {
        Random random = new Random();
        int rand = random.nextInt(max) % (max - min + 1) + min;
        ;
        return rand;
    }
    
    public static double randomFloat(double min, double max, int scale) {
        BigDecimal cha = new BigDecimal(Math.random() * (max - min) + min);
        return cha.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
 
    public static String getRandomNum(int num) {
        String number = "";
        for (int i = 0; i < num; i++) {
            int j = (int) (java.lang.Math.random() * 10);
            number += String.valueOf(j);
        }
        return number;
    }
 
    public static void main(String[] args) {
        System.out.println(RandomUtil.getRandomNum(4));
        System.out.println(RandomUtil.getRandomNum(2));
        System.out.println(RandomUtil.getRandomNum(2));
        System.out.println(RandomUtil.getRandomNum(2));
        System.out.println(RandomUtil.getRandomNum(2));
    }
}