自制随机工具类

持续更新(19.2.14)
萌新自己编写的随机工具类,说不定哪一天真的用上了emm

序号主要功能
1生成指定长度的随机字符(大小写字母,数字) 例:c5bGd92jok7ffg45
2你给我一个一维数组,我随机返回一个元素给你
3你给我一个二维数组,我随机返回一个元素给你
4在指定范围内随机生成一个浮点类型的数 例:4.5452454556561

代码

/**
 * ClassName : RandomUtils
 * Param : com.osc.itheima.utils
 * Create by Acachi-Akari
 * CreateDatetime : 2019/2/14 17:34
 * Description : 随机生成工具类
 **/
public class RandomUtils {
    //选择随机字符的返回类型
    public static final int NUM = 1003401;
    public static final int EN = 1003402;
    public static final int EN_LOW = 1003403;
    public static final int EN_UP = 1003404;
    public static final int NUM_EN_UP = 1003405;
    public static final int NUM_EN_LOW = 1003406;
    public static final int ALL = 1003407;
    static Character[][] ch = {
            {'0','1','2','3','4','5','6','7','8','9'},
            {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
            {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
    };

    /**
     * 字符随机生成
     * @param len 长度
     * @param type 返回类型
     * @return String
     */
    public static String genRanChar (int len , int type){
       String res = "";
        switch (type){
            //生成指定长度的随机数字
            case NUM:{
                for (int i = 0; i < len; i++) {
                    res += (int)(Math.random()*10);
                }
            };break;
            //生成指定长度的随机小写字母
            case EN_LOW:{
                for (int i = 0; i < len; i++) {
                    res += getARan(ch[1]);
                }
            };break;
            //生成指定长度的随机大小写字母
            case EN:{
                for (int i = 0; i < len; i++) {
                    res += ch[(int)(Math.random()*2+1)][(int)(Math.random()*26)];
                }
            };break;
            //生成指定长度的随机大写字母
            case EN_UP:{
                for (int i = 0; i < len; i++) {
                    res += getARan(ch[2]);
                }
            };break;
            //生成指定长度的随机小写字母 + 数字
            case NUM_EN_LOW:{
                for (int i = 0; i < len; i++) {
                    int index = (int)(Math.random()*2);
                    res += ch[index][(int)(Math.random()*ch[index].length)];
                }
            };break;
            //生成指定长度的随机大写字母 + 数字
            case NUM_EN_UP:{
                int[] temparr = {0,2};
                for (int i = 0; i < len; i++) {
                    int index = temparr[(int)(Math.random()*2)];
                    res += ch[index][(int)(Math.random()*ch[index].length)];
                }
            };break;
            //生成指定长度的随机大小写字母 + 数字
            case ALL:{
                for (int i = 0; i < len; i++) {
                    res += getARan(ch);
                }
            };break;
        }
        return res;
    }

    /**
     * 获取一维数组中的一个元素并返回
     * @param arr 一维数组
     * @return
     */
    public static Object getARan(Object[] arr){
        int index = (int)(Math.random()*arr.length);
        return arr[index];
    }

    /**
     * 获取二维数组中的一个元素并返回
     * @param arr 二维数组
     * @return
     */
    public static Object getARan(Object[][] arr){
        int index1 = (int)(Math.random()*arr.length);
        int index2 = (int)(Math.random()*arr[index1].length);
        return arr[index1][index2];
    }

    /**
     * 生成指定范围内的随机数
     * @param min 最小值
     * @param max 最大值
     * @return double类型
     */
    public static double ran(int min , int max){
        return Math.random() * (max - min + 1) + min;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值