关于一个生成验证码代码的问题
public class Udf {
private static final String VERIFY_CODES = "123456789";
public static void main(String[] args) {
String s=generateVerifyCode(5,VERIFY_CODES);
System.out.println(s);
}
public static String generateVerifyCode(int verifySize, String sources)
{
if (sources == null || sources.length() == 0)
{
sources = VERIFY_CODES;
}
int codesLen = sources.length();
Random rand = new Random(System.currentTimeMillis());
StringBuilder verifyCode = new StringBuilder(verifySize);
for (int i = 0; i < verifySize; i++)
{
verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
}
return verifyCode.toString();
}
}
这是一段生成验证码的代码 求助
问题1:verifyCode.append(sources.charAt(rand.nextInt(codesLen-1))); 这段代码后面为什么要-1 我把-1取消了 效果并没差别.
问题2:Random rand = new Random(System.currentTimeMillis()); 这里为什么要在后面获取一个当前的时间戳然后用随机 随机这个有什么作用?
问题3:verifyCode.append(sources.charAt(rand.nextInt(codesLen-1))); 这句代码看了很久 真的不懂在运行什么
求助各位高手 解我心结! 感谢