package com.fzm.mall.server.admin.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author wangp
 * @date 2021/5/13 16:21
 * @description 用字母和数字生成不重复的随机字符串
 * @since JDK 1.8
 */
public class RandomUtil {

    private static final String[] GENERATE_SOURCE = new String[]{
            "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",
    };
    private static final int STR_LEN = GENERATE_SOURCE.length;

    public static String generateByShuffle() {
        List<String> list = Arrays.asList(GENERATE_SOURCE);
        Collections.shuffle(list);
        StringBuilder randomStr = new StringBuilder();
        for (int i = 0; i < STR_LEN; i++) {
            randomStr.append(list.get(i));
        }
        return randomStr.substring(4, 10);
    }

    public static void main(String[] args) {
        System.err.println(generateByShuffle());
    }
}
