学而实习之 不亦乐乎

Java 生成随机字符串

2023-12-27 20:52:37

用字母和数字随机生成不重复的字符串需要满足如下条件:

  • 唯一性:确保每个字符串都是系统中独一无二的,使得可以根据此字符串进行反向推导。
  • 随机性:满足难于从字符串上推断出生成机制的要求。
  • 高效性:算法简单,时间复杂度低,或者不过度耗费系统资源。
  • 简洁性:用户可以方便的识别。

例如,我们平时看到的邀请码、短信验证码、快递代收点推送的取件码或者图形验证码,就是一组随机字符串,由此可见,随机字符串的应用非常广,老铁们需要了解一下她的生成机制。这些随机字符串的长度通常在6位左右,就是为了满足简洁性。当然,对人人皆知的java.util.UUID UUID.randomUUID().toString()就不再介绍了。

一、生成随机字符串

这里提供一个随机生成字符串的工具类,里面包括五种生成机制,源码如下。当然,为了达到不重复的目的,需要在数据库中创建一张专门维护已使用随机字符串的表,在生成随机字符串后,根据此字符串到中查重,如果存在,则继续生成新的字符串,直到拿到不重复的字符串为止。如果重复次数比较多,则可以适度增加随机字符串的长度,例如方法 generateByRandom(final int length),字符串长度length可以由业务实际需求确定。

/**
 * 用字母和数字生成不重复的随机字符串
 */
public class RandomUtil {
    /**
     * 字符源,可以剔除O、L、0和1,避免0和1与O和L混淆,这里没有剔除<br/>
     * 可以根据需要加入小写英文字母和特殊字符等
     */
    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;

    /**
     * 使用 Collections.shuffle 生成六位随机字符串
     *
     * @return
     */
    private 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) {
        for (int i = 0; i < 10; i++) {
            System.out.println(generateByRandom(6));
        }
        System.out.println(generateByShuffle() + "---" + STR_LEN);
    }

    /**
     * 生成数字和字母组合,字母区分大小写
     *
     * @param length 随机字符串的长度
     * @return
     */
    public static String generateByRandom(final int length) {
        StringBuilder randomSb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
 
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";

            // 字符串
            if ("char".equals(charOrNum)) {

                // 判断字母大小写
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                randomSb = randomSb.append((char) (choice + random.nextInt(26)));
            } else {
                randomSb = randomSb.append(random.nextInt(10));
            }
        }

        return randomSb.toString();
    }

    /**
     * 生成随机字符串,generateByRandom的简化版
     * @param count 随机字符串的长度
     * @param source 源字符集
     * @return
     */
    public static String doGenerate(int count, String[] source) {
        StringBuilder sb = new StringBuilder(count);
        int sourceLen = source.length;
        for (int i = 0; i < count; i++) {
            final int index = new Random().nextInt(sourceLen);
            sb.append(source[index]);
        }
        return sb.toString();
    }

    /**
     * @param begin 自增长序列
     * @return
     */
    public static String generateByOrder(final int begin) {
        List<String> src = Arrays.asList(GENERATE_SOURCE);
        Collections.shuffle(src);
        StringBuilder randomSb = new StringBuilder(6);
        int i4 = (begin) % 36;
        int i3 = (begin / (36)) % 36;
        int i2 = (begin / (36 * 36)) % 36;
        int i1 = (begin / (36 * 36 * 36)) % 36;
        int i0 = (begin / (36 * 36 * 36 * 36)) % 36;
        int i  = (begin / (36 * 36 * 36 * 36 * 36)) % 36;

        randomSb = randomSb.append(src.get(i0)).append(src.get(i1))
                .append(src.get(i2)).append(src.get(i3))
                .append(src.get(i4)).append(src.get(i));

        return randomSb.toString();
    }
}

第一种生成机制generateByShuffle()基于Collections.shuffle 生成六位随机字符串。generateByRandom(int length)借助Random()函数生成区分字母大小写的随机字符串,字符串长度可以根据入参自定义,提供了更灵活的生成机制。doGenerate(int count, String[] source) 是generateByRandom(int length)的简化版。第四种生成机制generateByOrder(int begin)需要在使用时维护一个自增长序列,保证入参begin是自增长的,举个例子:

public static void main(String[] args) {
    for (int i = 0; i < 100; i = i+10) {
        System.out.println(generateByOrder(i));
    }
}

第五种生成机制是直接使用randomAlphanumericc(final int count)函数,她位于org.apache.commons.lang3包下的RandomStringUtils类中,可以随机生成指定长度为count的字符串。

代码如下:

/**
 * @param count the length of random string to create
 * @return
 */
public static String randomAlphanumeric(int count) {
    return RandomStringUtils.randomAlphanumeric(count);
}

此方法需要导入如下依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

RandomStringUtils.randomAlphanumeric(count)生成一个指定长度为count的随机字符串,内容为大小写字母和0~9的数字。另外,RandomStringUtils类中的函数randomAscii(final int count)可以生成从ASCII 32到126组成的随机字符串,长度为count。