Java生成隨機(jī)字符串的代碼
引用Random類:
import java.util.Random;
程序代碼:
public static String getRandomString(int length) { //length表示生成字符串的長度
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
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();
}
x