package shop.alien.util.common;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * 生成随机数工具类
 *
 * @author ssk
 * @version 1.0
 * @date 2024/12/12 15:17
 */
public class RandomCreateUtil {

    /**
     * 生成随机数
     *
     * @param length 位数
     * @return 随机数
     */
    public static String getRandomNum(Integer length) {
        List<String> list = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
        Collections.shuffle(list);
        List<String> collect = list.stream().limit(length).collect(Collectors.toList());
        StringBuilder stringBuilder = new StringBuilder();
        collect.forEach(stringBuilder::append);
        return stringBuilder.toString();
    }

    /**
     * 生成随机数
     *
     * @param min 最小值
     * @param max 最大值
     * @return 随机数
     */
    public static Integer getRandomNum(Integer min, Integer max) {
        Random random = new Random();
        return random.nextInt((max - min) + 1) + min;
    }

    /**
     * 生成随机字符串
     *
     * @param length 长度
     * @return 字符串
     */
    public static String getRandomNStr(Integer length) {
        StringBuilder sb = new StringBuilder();
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (int i = 0; i < length; i++) {
            int index = ThreadLocalRandom.current().nextInt(chars.length());
            sb.append(chars.charAt(index));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        RandomCreateUtil randomCreateUtil = new RandomCreateUtil();
        System.out.println(randomCreateUtil.getRandomNStr(20));
    }
}
