public static String getRandomNumByLength(int length) {
String base = "ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < length; i++ ){
int number = random.nextInt( base.length() );
sb.append( base.charAt( number ) );
}
return sb.toString();
}
public String getRandomNumByLength(Integer randomNumLength, String type){
String rNum = String.valueOf(System.currentTimeMillis());
Integer typeLength = type.length();
int i = randomNumLength - rNum.length()-typeLength;
String nonceStr = type+rNum+ getRandomNumByLength(i);
return nonceStr;
}
案例: 生成16位数的随机数具体业务实现如下public static String getRandomNumByLength(int length) { String base = "ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; Random random = new Random(); StringBuffer sb = new StringBuffer(); .