Java Random Tool Class

Java Random Tool Class

java.util.Random
java.util.UUID

java.util.Random

//随机生成10个0-10之间的数字
public class Random {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 0; i < 10; i++) {
    
    
        //java.lang.Math类 random()方法,是返回大于0 小于1的随机小数(double)
            System.out.printf("%.0f%n",Math.random()*10);
        }

    }
}
运行结果:
4
6
0
10
9
8
6
4
3
10
for (int i = 0; i < 10; i++) {
    
    
        //java.lang.Math类 random()方法,是返回大于0 小于1的随机小数(double)
            System.out.println(Math.random());
        }
运行结果:
0.3495479534547913
0.2614015011210279
0.19513308613322844
0.7029237959289357
0.3669298923700868
0.6817109560177244
0.20743644889426227
0.05549096731929326
0.3589773723763797
0.16557365319219486
        Random rand = new Random();
        System.out.println(rand.nextBoolean());//随机生成布尔型
        System.out.println(rand.nextDouble());//随机生成Double型
运行结果:    
true
0.36581163987123055
        for (int a=0;a<10;a++){
    
    
           // rand.nextInt(5);//随即范围是0--4
           // System.out.printf("%d", rand.nextInt(5));
            /*   //10--15随机数  方法一
            System.out.println(rand.nextInt(10,16));*/

            //10--15随机数  方法二
            int t= rand.nextInt(6)+10;
            System.out.println(t);
        }
运行结果:
15
11
15
15
13
14
15
15
15
13

java.util.UUID

System.out.println(UUID.randomUUID());
运行结果:
4c9e5579-5d23-4b74-8342-c0ec32c1c081

Guess you like

Origin blog.csdn.net/qq_56015865/article/details/128976817