• 随机数工具类


    package com.JUtils.math;
    
    import java.util.Random;
    
    /**
     * 随机数工具类
     *
     */
    public class RandomUtils {
        private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static final String NUMBER_CHAR = "0123456789";
    
        /**
         * 获取定长的随机数,包含大小写、数字
         *
         * @param length
         *                 随机数长度
         * @return
         */
        public static String generateString(int length) {
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            for (int i = 0; i < length; i++) {
                    sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
            }
            return sb.toString();
        }
    
        /**
         * 获取定长的随机数,包含大小写字母
         *
         * @param length
         *                 随机数长度
         * @return
         */
        public static String generateMixString(int length) {
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            for (int i = 0; i < length; i++) {
                    sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length())));
            }
            return sb.toString();
        }
    
        /**
         * 获取定长的随机数,只包含小写字母
         *
         * @param length
         *                 随机数长度
         * @return
         */
        public static String generateLowerString(int length) {
            return generateMixString(length).toLowerCase();
        }
    
        /**
         * 获取定长的随机数,只包含大写字母
         *
         * @param length
         *                 随机数长度
         * @return
         */
        public static String generateUpperString(int length) {
            return generateMixString(length).toUpperCase();
        }
    
        /**
         * 获取定长的随机数,只包含数字
         *
         * @param length
         *                 随机数长度
         * @return
         */
        public static String generateNumberString(int length){
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            for (int i = 0; i < length; i++) {
                    sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length())));
            }
            return sb.toString();
        }
    
    }
  • 相关阅读:
    刷题-力扣-223. 矩形面积
    刷题-力扣-135. 分发糖果
    刷题-力扣-134. 加油站
    刷题-力扣-343. 整数拆分
    刷题-力扣-162. 寻找峰值
    刷题-力扣-371. 两整数之和
    20191114-2 Beta事后诸葛亮会议
    Beta阶段贡献分配
    扛把子组20191114-4 Beta发布用户使用报告
    beta 2/2 阶段中间产物提交
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11276334.html
Copyright © 2020-2023  润新知