生成指定长度的随机字符

public static String getRandomString(int length) { // length是随机字符串长度
    String base = "abcdefghijklmnopqrstuvwxyz0123456789";//字符自己定义
    Random random = new Random();
    StringBuffer sb = new StringBuffer();//synchronized
    for (int i = 0; i < length; i++) {
        int number = random.nextInt(base.length());
        sb.append(base.charAt(number));
    }
    return sb.toString();
}

 

你可能感兴趣的:(生成指定长度的随机字符)