Java生成随机邀请码

版权声明:谁不是在与生活苦战。狼性的年级,就不要做个俗人 https://blog.csdn.net/Axela30W/article/details/87713954
Java生成随机10位不重复邀请码
	public static void main(String[] args) {
	        for (int i = 0; i < 10; i++) {
	            System.out.println(createShareCode());
	        }
	    }

    /**
     * 生成邀请码
     *
     * @return
     */
    private static String createShareCode() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer code = new StringBuffer("");
        Random r = new Random();
        while (count < 10) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                code.append(str[i]);
                count++;
            }
        }
        return code.toString();
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Axela30W/article/details/87713954
今日推荐