Java 随机数工具


public class RandomUtil {
    public static final int ANY_CHARACTER = 0;
    public static final int ONLY_LOWER = 1;
    public static final int ONLY_UPPER = 2;
    public static final int ONLY_LOWER_UPPER = 3;
    public static final int ONLY_NUMBER = 4;
    private static final int DEFAULT_LENTH = 8;
    private static int low = 1;
    private static int high = 10;
    private static final int BUFFER_SIZE = 101;
    private static final int BUFFER_SIZE_CHAR = 72;
    private static double[] buffer = new double[101];
    private static char[] bufferChar = new char[72];

    static {
        for(int i = 0; i < 101; ++i) {
            buffer[i] = Math.random();
        }

        initCharBuffer();
    }

    private static void initCharBuffer() {
        String bufferString = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        int length = bufferString.length();

        for(int i = 0; i < length; ++i) {
            bufferChar[i] = bufferString.charAt(i);
        }

    }

    public RandomUtil() {
    }

    public RandomUtil(int low, int high) {
        low = low;
        high = high;
    }

    public static int nextIntRandom() {
        int r = low + (int)((double)(high - low + 1) * nextRandom());
        return r;
    }

    public static int nextIntRandom(int _low, int _high) {
        low = _low;
        high = _high;
        return nextIntRandom();
    }

    private static double nextRandom() {
        int pos = (int)(Math.random() * 101.0D);
        if (pos == 101) {
            pos = 100;
        }

        double value = buffer[pos];
        buffer[pos] = Math.random();
        return value;
    }

    public static String nextStringRandom(int length, int type) {
        switch(type) {
        case 1:
            low = 36;
            high = 61;
            break;
        case 2:
            low = 10;
            high = 35;
            break;
        case 3:
            low = 10;
            high = 61;
            break;
        case 4:
            low = 0;
            high = 9;
            break;
        default:
            low = 0;
            high = 61;
        }

        StringBuffer value = new StringBuffer();

        for(int k = 0; k < length; ++k) {
            int j = nextIntRandom();
            value.append(bufferChar[j]);
        }

        return value.toString();
    }

    public static String nextStringRandom() {
        return nextStringRandom(8, 5);
    }
	
	 public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();

        // 参数length,表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

	public static String getStringRandom(int length) {
        String val = "";
        java.util.Random random = new java.util.Random();

        // 参数length,表示生成几位随机数
        for (int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
	
    public static void main(String[] args) {
         System.out.println("type0 == " + nextStringRandom(10, 0));
        System.out.println("type1 == " + nextStringRandom(10, 1));
        System.out.println("type2 == " + nextStringRandom(10, 2));
        System.out.println("type3 == " + nextStringRandom(10, 3));
        System.out.println("type4 == " + nextStringRandom(10, 4));
        System.out.println("int == " + nextIntRandom());
        System.out.println("int == " + nextIntRandom());
        System.out.println("int == " + nextIntRandom(50,100));
        System.out.println("int == " + nextRandom());
        System.out.println("int == " + nextRandom());
    }
}

最后提个工具类:cn.hutool.core.util.RandomUtil

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值