Generating unique ids with the max length of 3 digits/letters/simbols

前端 未结 3 1684
迷失自我
迷失自我 2021-01-17 08:12

I have a list of 75200 words. I need to give a \'unique\' id to each word, and the length of each id could be 3 letters or less. I can use numbers, letters or even symbols b

3条回答
  •  [愿得一人]
    2021-01-17 08:23

    You could create a method that basically converts a decimal number to a base of your choice. Here I have 46 symbols for example, which gives 97336 unique sequences:

    private static final String[] symbols = { "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", "!", "@", "#", "$", "%", "^", "&",
            "*", "~", "?" };
    public static String getSequence(final int i) {
        return symbols[i / (symbols.length * symbols.length)] + symbols[(i / symbols.length) % symbols.length]
                + symbols[i % symbols.length];
    }
    

提交回复
热议问题