怎么样用java代码自动生成不重复的含有数字和字母的随机码
1个回答
展开全部
package tests;
/**
* 随机生成32位字符串
*/
public class String32 {
public static String random(int length){//传入的字符串的长度
StringBuilder builder = new StringBuilder(length);
for(int i = 0; i < length; i++){
int r = (int) (Math.random()*3);
int rn1=(int)(48+Math.random()*10);
int rn2=(int)(65+Math.random()*26);
int rn3=(int)(97+Math.random()*26);
switch(r){
case 0:
builder.append((char)rn1);
break;
case 1:
builder.append((char)rn2);
break;
case 2:
builder.append((char)rn3);
break;
}
}
return builder.toString();
}
public static void main(String[] args){
String s = String32.random(32);
System.out.println(s);
}
}
/**
* 随机生成32位字符串
*/
public class String32 {
public static String random(int length){//传入的字符串的长度
StringBuilder builder = new StringBuilder(length);
for(int i = 0; i < length; i++){
int r = (int) (Math.random()*3);
int rn1=(int)(48+Math.random()*10);
int rn2=(int)(65+Math.random()*26);
int rn3=(int)(97+Math.random()*26);
switch(r){
case 0:
builder.append((char)rn1);
break;
case 1:
builder.append((char)rn2);
break;
case 2:
builder.append((char)rn3);
break;
}
}
return builder.toString();
}
public static void main(String[] args){
String s = String32.random(32);
System.out.println(s);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询