• java生成随机数


    package com.rd.p2p.common.util;
    
    import java.util.Iterator;
    import java.util.LinkedHashSet;
    import java.util.Random;
    import java.util.Set;
    import java.util.UUID;
    
    /**
     * 工具类-随机数
    
     */
    public class RandomUtil {
    
        /**
         * 获取从a至z,长度为length随机数
         * 
         * @return
         */
        public static String getRandomStr(int length) {
            String base = "abcdefghijklmnopqrstuvwxyz";
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                int number = random.nextInt(base.length());
                sb.append(base.charAt(number));
            }
            return sb.toString();
        }
    
        /**
         * 获取范围内int值
         * 
         * @param 获取范围内int值
         * @return
         */
        public static int getRandomRange(int max, int min) {
            return (int) (Math.random() * (max - min) + min);
        }
    
        /**
         * 获取随机长度随机字符
         * 
         * @param length base
         * @return
         */
        public static String getRandomString(int length, String base) { // length表示生成字符串的长度
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                int number = random.nextInt(base.length());
                sb.append(base.charAt(number));
            }
            return sb.toString();
        }
    
        /**
         * 获取随机长度随机字符
         * 
         * @param length
         * @return
         */
        public static String getRandomString(int length) { // length表示生成字符串的长度
            String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                int number = random.nextInt(base.length());
                sb.append(base.charAt(number));
            }
            return sb.toString();
        }
    
        /**
         * 获取随机长度随机数字
         * 
         * @param length
         * @return
         */
        public static String getRandomNumString(int length) { // length表示生成字符串的长度
            String base = "0123456789";
            Random random = new Random();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < length; i++) {
                int number = random.nextInt(base.length());
                sb.append(base.charAt(number));
            }
            return sb.toString();
        }
    
        /**
         * 返回随机数组
         * 
         * @param start 开始值
         * @param end 结束值
         * @return
         */
        public static int[] getRangRandom(int start, int end) {
            return getRangRandom(start, end, end - start + 1);
        }
    
        /**
         * 返回指定范围指定个数的不重复随机数。
         * 
         * @param start
         * @param end
         * @param num
         * @return
         */
        public static int[] getRangRandom(int start, int end, int num) {
    
            int length = end - start + 1;
            // 参数不合法
            if (length < 1 || num > length) {
                return null;
            } else {
                int[] numbers = new int[length];
                int[] result = new int[num];
                // 循环赋初始值
                for (int i = 0; i < length; i++) {
                    numbers[i] = i + start;
                }
                Random random = new Random();
                // 取randomMax次数
                for (int i = 0; i < num; i++) {
                    // 随机获取取数的位置
                    int m = random.nextInt(length - i) + i;
                    result[i] = numbers[m];
                    // 交换位置
                    int temp = numbers[m];
                    numbers[m] = numbers[i];
                    numbers[i] = temp;
                }
                return result;
            }
        }
    
        /*
         * 生成6位随机数验证码
         */
        public static String code() {
            Set<Integer> set = getRandomNumber();
            Iterator<Integer> iterator = set.iterator();
            String temp = "";
            while (iterator.hasNext()) {
                temp += iterator.next();
            }
            return temp;
        }
    
        public static Set<Integer> getRandomNumber() {
            Set<Integer> set = new LinkedHashSet<Integer>();
            Random random = new Random();
            while (set.size() < 6) {
                set.add(random.nextInt(10));
            }
            return set;
        }
        
        public static String[] chars = new String[] { "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", "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" };
    
    
        public static String getShortUUID(int num) {
            StringBuffer shortBuffer = new StringBuffer();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            for (int i = 0; i < num; i++) {
                String str = uuid.substring(i * 4, i * 4 + 4);
                int x = Integer.parseInt(str, 16);
                shortBuffer.append(chars[x % 0x3E]);
            }
            return shortBuffer.toString();
        }
        
    }
  • 相关阅读:
    redis官方网站及文档
    kafka 官网帮助文档
    elasticsearch 官方入门 及 API
    解决Maven出现Plugin execution not covered by lifecycle configuration 错误
    linux下scp用法
    Spring AOP 实现原理
    深入浅出spring IOC中三种依赖注入方式
    Servlet API 中文版
    【转】httpservlet 文章
    jsp request 对象详解
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6956636.html
Copyright © 2020-2023  润新知