zoukankan      html  css  js  c++  java
  • 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();
        }
        
    }
  • 相关阅读:
    【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用
    【iCore4 双核心板_ARM】例程十一:DMA实验——存储器到存储器的传输
    【iCore1S 双核心板_FPGA】例程八:触发器实验——触发器的使用
    【iCore4 双核心板_ARM】例程十:RTC实时时钟实验——显示时间和日期
    【iCore4 双核心板_ARM】例程九:ADC实验——电源监控
    WebBrowser的Cookie操作之流量刷新机
    网站受攻击的常用手段
    八爪鱼招标网的百度权重升为2了,独立IP也从0快速发展为1000
    网站添加数据出错,原来是MS SQL Server2008日志文件占据空间过大导致的
    当你的网站被疯狂攻击时你能做什么?
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6956636.html
Copyright © 2011-2022 走看看