随机工具类

随机工具类

import java.util.Random;
import java.util.UUID;

public static void main(String[] args) {

        //随机id(UUID)
        UUID id = UUID.randomUUID();
        System.out.println(id);
        System.out.println(id.toString().length());//36位
        //68ed8d3f-a7b2-41eb-aad2-3a5359c3199c  随机书橱  不存在重复性

        //随机类Random
        Random rand = new Random();

        //true  false   真假随机
        System.out.println(rand.nextBoolean());//true  false

        //0~5随机整数
        System.out.println(rand.nextInt(6));

        System.out.println(rand.nextDouble());
        System.out.println(rand.nextFloat());
        System.out.println(rand.nextLong());
        System.out.println(rand.nextGaussian());


        //0~9随机整数
        for (int i = 0; i <10 ; i++) {
            System.out.print(rand.nextInt(10));
        }
        //Random rand = new Random();
        System.out.println(rand.nextInt(10));

        //随机颜色
        //Random rand = new Random();
        int rr = rand.nextInt(256);
        int gg = rand.nextInt(256);
        int bb = rand.nextInt(256);
        System.out.printf("#%02x%02x%02x%n", rr, gg, bb);

    }

在这里插入图片描述

public static void main(String[] args) {

        //随机选人
        String[] stu=new String[]{"小张","阿辉","小龙","jack","小王","long","Dragon"};
        int size=stu.length;
        System.out.println(size);
        var rand=new Random();
        int index=rand.nextInt(stu.length);
        System.out.println(index);
        String info=String.format("有%d位学生,本次选中的学生是:%s",size,stu[index]);
        System.out.println(info);
    }

image-20210729211429344

    public static void main(String[] args) {

        //随机输出10以内四则运算
        for (int i = 0; i < 10; i++) {
            Random rand = new Random();
            int num1 = rand.nextInt(10);
            int num2 = rand.nextInt(10);
            String[] str = new String[]{"+", "-", "x", "÷"};
            int f1 = rand.nextInt(str.length)
            String info = String.format("%d %s %d =", num1, str[f1], num2);
            System.out.println(info);
        }
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值