产生随机字符串(a-z A-Z 0-9)

实现方法一:
[quote]
public class RandomFileName {
/**
* 产生一个随机的字符串
*
* @param 字符串长度
* @return
*/
public static String getRandomString(int length) {
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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 static void main(String[] args) {
System.out.println(RandomFileName.getRandomString(5));
}
}
[/quote]

实现方法二:
[quote]
public class RandomFileName2 {
/**
* 产生一个随机的字符串
*
* @param 字符串长度
* @return
*/
public static String getRandomString(int length) {
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i int number=random.nextInt(3);
long result=0;
switch(number){
case 0:
result = Math.round(Math.random()*25+65);
sb.append(String.valueOf((char)result));
break;
case 1:
result = Math.round(Math.random()*25+97);
sb.append(String.valueOf((char)result));
break;
case 2:
sb.append(String.valueOf(new Random().nextInt(10)));
break;
}
}
return sb.toString();
}

public static void main(String[] args) {
System.out.println(RandomFileName2.getRandomString(10));
}
}

[/quote]

你可能感兴趣的:(java)