随机数小工具类


package com.*.*.utils;

import java.util.Random;

import org.apache.commons.lang3.StringUtils;

public class RandomUtils {
    public static final String NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String NUMBERS = "0123456789";
    public static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
    public static final String CAPITAL_HEX_NUMBERS = "0123456789ABCDEF";
    public static final String LOWER_HEX_NUMBERS = "0123456789abcdef";
    public static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String SPECIAL_CHARACTER = "~!@#$%^&*<>/";
    
    
    

    private RandomUtils() {
        throw new AssertionError();
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase, lowercase
     * letters and numbers
     * 
     * @param length
     *            length
     * @return RandomUtils
     */
    public static String getRandomNumbersAndLetters(int length) {
        return getRandom(NUMBERS_AND_LETTERS, length);
    }

    /**
     * get a fixed-length random string, its a mixture of numbers
     * 
     * @param length
     *            length
     * @return RandomUtils
     */
    public static String getRandomNumbers(int length) {
        return getRandom(NUMBERS, length);
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase and
     * lowercase letters
     * 
     * @param length
     *            length
     * @return RandomUtils
     */
    public static String getRandomLetters(int length) {
        return getRandom(LETTERS, length);
    }

    /**
     * get a fixed-length random string, its a mixture of uppercase letters
     * 
     * @param length
     *            length
     * @return CapitalLetters
     */
    public static String getRandomCapitalLetters(int length) {
        return getRandom(CAPITAL_LETTERS, length);
    }

    /**
     * get a fixed-length random string, its a mixture of lowercase letters
     * 
     * @param length
     *            length
     * @return get a fixed-length random string, its a mixture of lowercase
     *         letters
     */
    public static String getRandomLowerCaseLetters(int length) {
        return getRandom(LOWER_CASE_LETTERS, length);
    }

    /**
     * 获取随机小写十六进制数
     * 
     * @param length
     * @return
     */
    public static String getRandomLowerHexNumbers(int length) {
        return getRandom(LOWER_HEX_NUMBERS, length);
    }

    /**
     * 获取随机大写十六进制数
     * 
     * @param length
     * @return
     */
    public static String getRandomCapitalHexNumbers(int length) {
        return getRandom(CAPITAL_HEX_NUMBERS, length);
    }

    /**
     * get a fixed-length random string, its a mixture of chars in source
     * 
     * @param source
     *            source
     * @param length
     *            length
     * @return get a fixed-length random string, its a mixture of chars in
     *         source
     */
    public static String getRandom(String source, int length) {
        return StringUtils.isEmpty(source) ? null : getRandom(
                source.toCharArray(), length);
    }

    /**
     * get a fixed-length random string, its a mixture of chars in sourceChar
     * 
     * @param sourceChar
     *            sourceChar
     * @param length
     *            length
     * @return get a fixed-length random string, its a mixture of chars in
     *         sourceChar
     */
    public static String getRandom(char[] sourceChar, int length) {
        if (sourceChar == null || sourceChar.length == 0 || length < 0) {
            return null;
        }

        StringBuilder str = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            str.append(sourceChar[random.nextInt(sourceChar.length)]);
        }
        return str.toString();
    }

    /**
     * 
     * @param max
     *            接受的数值
     * @return 返回一个随机的数值
     */
    public static int getRandom(int max) {

        return getRandom(0, max);
    }

    /**
     * 
     * @param min
     *            最小
     * @param max
     *            最大
     * @return 返回一个范围的数值
     */
    public static int getRandom(int min, int max) {

        if (min > max) {
            return 0;
        }
        if (min == max) {
            return min;
        }
        return min + new Random().nextInt(max - min);
    }

    /**
     * Shuffling algorithm, Randomly permutes the specified array using a
     * default source of randomness
     * 
     * @param objArray
     *            数组
     * @return 从新的数组
     */
    public static boolean shuffle(Object[] objArray) {
        if (objArray == null) {
            return false;
        }

        return shuffle(objArray, getRandom(objArray.length));
    }

    /**
     * Shuffling algorithm, Randomly permutes the specified array
     * 
     * @param objArray
     *            数组
     * @param shuffleCount
     *            洗的个数
     * @return 是否成功
     */
    public static boolean shuffle(Object[] objArray, int shuffleCount) {
        int length;
        if (objArray == null || shuffleCount < 0
                || (length = objArray.length) < shuffleCount) {
            return false;
        }

        for (int i = 1; i <= shuffleCount; i++) {
            int random = getRandom(length - i);
            Object temp = objArray[length - i];
            objArray[length - i] = objArray[random];
            objArray[random] = temp;
        }
        return true;
    }

    /**
     * Shuffling algorithm, Randomly permutes the specified int array using a
     * default source of randomness
     * 
     * @param intArray
     *            数组
     * @return 洗牌之后
     */
    public static int[] shuffle(int[] intArray) {
        if (intArray == null) {
            return null;
        }

        return shuffle(intArray, getRandom(intArray.length));
    }

    /**
     * Shuffling algorithm, Randomly permutes the specified int array
     * 
     * @param intArray
     *            数组
     * @param shuffleCount
     *            范围
     * @return 新的数组
     */
    public static int[] shuffle(int[] intArray, int shuffleCount) {
        int length;
        if (intArray == null || shuffleCount < 0
                || (length = intArray.length) < shuffleCount) {
            return null;
        }

        int[] out = new int[shuffleCount];
        for (int i = 1; i <= shuffleCount; i++) {
            int random = getRandom(length - i);
            out[i - 1] = intArray[random];
            int temp = intArray[length - i];
            intArray[length - i] = intArray[random];
            intArray[random] = temp;
        }
        return out;
    }
    
    public static String getRandPwd(){
        StringBuffer pwd=new StringBuffer();
        Random rand = new Random();
        //随机取4个大写字母
        for (int i = 0; i < 4; i++) 
        {
            pwd.append(UPPER_CASE.charAt(rand.nextInt(26)) + "");
        }
        //随机取一个特殊字符
        for(int j=0;j<1;j++){
            pwd.append(SPECIAL_CHARACTER.charAt(rand.nextInt(12)));
        }
        //随机取三个数字
        for(int k=0;k<3;k++){
            pwd.append(NUMBERS.charAt(rand.nextInt(10)));
        }
        return pwd.toString();
    }
    
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值