package com.sy.coinage.core.util;

import java.util.Random;

/**
 * @Description: 随机数工具类
 * @author: lug 2021年6月10日
 */
public class RandomUtil {

    public static final String VERIFY_CODES = "123456789abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

    /**
     * 产生字符串随机数
     *
     * @param length 字符串长度
     * @return
     */
    public static String randomStr(int length) {
        int codesLen = VERIFY_CODES.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(codesLen - 1)));
        }
        return verifyCode.toString();
    }

    /**
     * 产生6位数字串随机数
     *
     * @return
     */
    public static final String random6Int() {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (999999 - 100000 + 1) + 100000 + "";
    }

    /**
     * 产生数字串随机数
     *
     * @param length 长度
     * @return
     */
    public static final String randomInt(Integer length) {
        String result = "";
        for (int i = 0; i < length; i++) {
            Double rand = Math.random();
            result += (int) (rand * 10);
        }
        return result;
    }

    /**
     * 产生小于最大数数字随机数
     *
     * @param max 最大数
     * @return
     */
    public static final int randomMax(Integer max) {
        Random random = new Random();
        int result = random.nextInt(max);
        return result;
    }

}
