随机生成16位的验证码:
import java.util.Random;
public class Test02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getChar(16));
}
public static String getChar(int length) {
char[] ss = new char[length];
int i=0;
while(i<length) {
int f = (int) (Math.random()*3);
if(f==0)
ss[i] = (char) ('A'+Math.random()*26);
else if(f==1)
ss[i] = (char) ('a'+Math.random()*26);
else
ss[i] = (char) ('0'+Math.random()*10);
i++;
}
String str=new String(ss);
return str;
}
}
转载:https://blog.csdn.net/quwei7515/article/details/7926105
用StringBuilder随机生成一个16位的密码,可以是大小写字母或数字。
import java.util.Random;
public class Test03 {
public static void main(String[] args) {
Random rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16; i++) {
int f = (int) (Math.random()*62);
if(f<10) {
sb.append(f);
}else if(f<36) {
sb.append((char)(f + 'A'-10));
}else {
sb.append((char)(f + 'a'-36));
}
}
System.out.println(sb);
}
}
转载:https://www.cnblogs.com/jeffqing/archive/2012/07/28/2612570.html
自己的方法:第二种:
import java.util.Random;
public class Test04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();
StringBuilder sb = new StringBuilder();
String strAll = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 16; i++) {
int f = (int) (Math.random()*62);
sb.append(strAll.charAt(f));
}
System.out.println(sb);
}
}