使用Random生成随机数
public static String getStringRandom() {
Random random = new Random();
String str = String.valueOf(random.nextInt(9));
for (int i = 0; i < 5; i++) {
str += random.nextInt(9);
}
return str;
}
使用Math生成随机数
public static String getStringRandom() {
return String.valueOf(Math.random()).substring(2, 8);
}
使用ThreadLocalRandom生成随机数
public static String getStringRandom() {
ThreadLocalRandom random = ThreadLocalRandom.current();
String str = String.valueOf(random.nextInt(10));
for (int i = 0; i < 5; i++) {
str += random.nextInt(10);
}
return str;
}
使用SecureRandom生成随机数
public static String getStringRandom() throws NoSuchAlgorithmException {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
String str = String.valueOf(random.nextInt(10));
for (int i = 0; i < 5; i++) {
str += random.nextInt(10);
}
return str;
}
标签:正整数,String,getStringRandom,random,生成,nextInt,static,随机,str From: https://www.cnblogs.com/xfeiyun/p/17856029.html