當前位置:網站首頁>生成隨機數和隨機串

生成隨機數和隨機串

2022-01-26 22:45:26 至學者

一 實例

    /**
     * 生成隨即4比特隨機數
     */
    public String getRandomNum() {
        return (int) ((Math.random() * 9 + 1) * 1000) + "";
    }

二 生成隨機串

/**
 * 根據用戶id生成不重複邀請碼 小於id 754137930為6比特,超出後邀請碼比特數將大於6
 **/
public class generateCode {
    /** 邀請碼最小長度,生成邀請碼小於該字段,自動補長  **/
    private static final int MIN_CODE_LENGTH_1 = 10;
    /** 比特數不足時,充當分隔,該字段為保持唯一性,不放入下方的列錶  **/
    private static final String STOP_CHAR_1 = "0";
    private static final String[] CHARS_1 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"};

    /** 兌換碼最小長度,生成邀請碼小於該字段,自動補長  **/
    private static final int MIN_CODE_LENGTH_2 = 8;
    /** 比特數不足時,充當分隔,該字段為保持唯一性,不放入下方的列錶  **/
    private static final String STOP_CHAR_2 = "U";
    private static final String[] CHARS_2 = new String[]{"Q","W","E","8","A","S","2","D","Z","X","9","C","7","P","5","K","3","M","J","F","R","4","V","Y","L","T","N","6","B","G","H"};

    /**
     * 默認使用配置一
     * */
    private static int MIN_CODE_LENGTH = MIN_CODE_LENGTH_1;
    private static String STOP_CHAR = STOP_CHAR_1;
    private static String[] CHARS = CHARS_1;
    private static int OFFSET = CHARS_1.length - 1;

    public generateCode(int type) {
        if (type == 2) {
            // 兌換碼
            MIN_CODE_LENGTH = MIN_CODE_LENGTH_2;
            STOP_CHAR = STOP_CHAR_2;
            CHARS = CHARS_2;
            OFFSET = CHARS_2.length - 1;
        }
    }

    /**
     * 根據 id 生成邀請碼
     * 如果是 6 比特的邀請碼只能支持 754137930 7億5千萬用戶, 超出的id 會成為7比特甚至更長的邀請碼
     * @param id 用戶id
     * @return 邀請碼字符串
     */
    public static String id2ShareCode(long id, Integer codeMinLength) {
        if (codeMinLength == null) {
            codeMinLength = MIN_CODE_LENGTH;
        }
        String code = int2chars(id);
        int tailLength = codeMinLength - code.length();
        if (tailLength > 1) {
            code = code + STOP_CHAR + codeTail(tailLength - 1);
        } else if (tailLength == 1) {
            code = code + STOP_CHAR;
        }
        return code;
    }

    /**
     * 根據 id 生成邀請碼
     * 如果是 6 比特的邀請碼只能支持 754137930 7億5千萬用戶, 超出的id 會成為7比特甚至更長的邀請碼
     * @param id 用戶id
     * @return 邀請碼字符串
     */
    public String id2ShareCode(long id) {
        int codeMinLength = MIN_CODE_LENGTH;
        String code = int2chars(id);
        int tailLength = codeMinLength - code.length();
        if (tailLength > 1) {
            code = code + STOP_CHAR + codeTail(tailLength - 1);
        } else if (tailLength == 1) {
            code = code + STOP_CHAR;
        }
        return code;
    }

    /**
     * 根據邀請碼 獲取 id
     * @param code 邀請碼
     * @return 用戶id
     */
    public static long shareCode2id(String code) {
        int inx = code.indexOf(STOP_CHAR);
        if (inx > 0) {
            code = code.substring(0, inx);
        }
        return chars2int(code);
    }

    /**
     * 獲取補長的邀請碼(隨機)
     * @param len 需要的長度
     * */
    private static String codeTail(int len) {
        String res = "";
        Random r = new Random();
        for (int i = 0; i < len; i++) {
            res += CHARS[r.nextInt(OFFSET)];
        }
        return res;
    }

    private static String int2chars(long id) {
        long x = id / OFFSET;
        long remainder = id % OFFSET;
        if (x == 0) {
            return CHARS[(int) id];
        } else if (x < OFFSET) {
            return CHARS[(int) x] + CHARS[(int) remainder];
        } else {
            return int2chars(x) + CHARS[(int) remainder];
        }
    }

    private static long chars2int(String chars) {
        long res = 0;
        int codeLen = chars.length();
        List<String>  totalCharsList = Arrays.asList(CHARS);

        for (int i = 0; i < codeLen; i++) {
            String a = chars.substring(i, i+1);
            if (STOP_CHAR.equals(a)) {
                break;
            }
            if (totalCharsList.contains(a)) {
//                res += (long) totalCharsList.indexOf(a) * (Math.pow(OFFSET, (codeLen - i - 1)));
                res = res * OFFSET + totalCharsList.indexOf(a);
            } else {
                res = 0;
                break;
            }
        }
        return res;
    }
}

二 生成隨機串的問題

        由於STOP_CHAR為靜態變量,初始值為STOP_CHAR_1 = "0",但是在滿足type=2的時候STOP_CHAR 重新被賦值為STOP_CHAR_2="U",此時如果再使用不滿足type=2的條件的時候,按常理來說STOP_CHAR的值應該為STOP_CHAR_1 = "0",兩次使用同一個對象的時候,此時的STOP_CHAR值仍然為STOP_CHAR_2="U"。

        部分修改


    private static int MIN_CODE_LENGTH = 0;
    private static String STOP_CHAR = "";
    private static String[] CHARS = ""
    private static int OFFSET = "";

    public generateCode(int type) {
        if (type == 1) {
            // 7比特字符串
            MIN_CODE_LENGTH = MIN_CODE_LENGTH_1;
            STOP_CHAR = STOP_CHAR_1;
            CHARS = CHARS_1;
            OFFSET = CHARS_1.length - 1;
        }
        if (type == 2) {
            // 兌換碼
            MIN_CODE_LENGTH = MIN_CODE_LENGTH_2;
            STOP_CHAR = STOP_CHAR_2;
            CHARS = CHARS_2;
            OFFSET = CHARS_2.length - 1;
        } 
    }

        或者

    private static int MIN_CODE_LENGTH = 0;
    private static String STOP_CHAR = "";
    private static String[] CHARS = ""
    private static int OFFSET = "";

    
    public generateCode() {
            // 7比特字符串
            MIN_CODE_LENGTH = MIN_CODE_LENGTH_1;
            STOP_CHAR = STOP_CHAR_1;
            CHARS = CHARS_1;
            OFFSET = CHARS_1.length - 1;
        }

    public generateCode(int type) {
            // 兌換碼
            MIN_CODE_LENGTH = MIN_CODE_LENGTH_2;
            STOP_CHAR = STOP_CHAR_2;
            CHARS = CHARS_2;
            OFFSET = CHARS_2.length - 1;
    }

        待後續一一分析,如有問題,希望讀者留言告知,謝謝!

版權聲明
本文為[至學者]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201262245263842.html

隨機推薦