Java随机工具类

Java随机工具类

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值