Java新手请教:如果产生一个由100个随机数字或英文字符组成字符串?
诚心求教?
------解决方案--------------------
/**
* 产生随机字符串
* */
private static Random randGen = null;
private static char[] numbersAndLetters = null;
public static final String randomString(int length) {
if (length < 1) {
return null;
}
if (randGen == null) {
randGen = new Random();
numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
//numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
}
char [] randBuffer = new char[length];
for (int i=0; i<randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
//randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
}
return new String(randBuffer);
}
//调用此方法randomString(int),int是字符串的长度,即可产生指定长度的随机字符串。
------解决方案--------------------我的一个简单的随机数
http://yijianfengvip.blog.163.com/blog/static/1752734322011730113047841/