编程实现用户名和密码自动生成【可以自己指定长度】

文章来源:http://biancheng.dnbcw.info/java/163717.html


也不是不可以,你用个随机数random类生成string随机数,用两个string包装类去赋值,不就可以了

Java code
    
    
    
    
/** * 产生随机字符串 * */ private static Random randGen = null; private static char[] numbersAndLetters = null; private static Object initLock = new Object(); public static final String randomString(int length) { if (length < 1) { return null; } if (randGen == null) { synchronized (initLock) { 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); }

------回答---------

------其他回答(20分)---------

也不是不可以,你用个随机数random类生成string随机数,用两个string包装类去赋值,不就可以了
Java code
    
    
    
    
/** * 产生随机字符串 * */ private static Random randGen = null; private static char[] numbersAndLetters = null; private static Object initLock = new Object(); public static final String randomString(int length) { if (length < 1) { return null; } if (randGen == null) { synchronized (initLock) { 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); }

你可能感兴趣的:(编程实现用户名和密码自动生成【可以自己指定长度】)