Random Password Generation





7
Date Submitted Fri. Feb. 17th, 2006 4:26 AM
Revision 2 of 2
Beginner ramanathank
Tags Java
Comments 7 comments
Change logs

-> length calculated from the array
-> charachters will not be repeated in the password


/* This method is used to generate a random password
*    @ params int - length of the password
*    @ return  String - random password
*/


public String generateRandomPassword(int length){
        String  passarray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        long range = passarray.length();
        String passwd = "";
        for(int i = 0; i < length; ){
            Random generator = new Random();
            int rnd = (int)(range * generator.nextDouble());
            String ch = passarray.substring(rnd,rnd+1);
            if(passwd.indexOf(ch)==-1){
                passwd = passwd + ch;
                i++;
            }
        }
        return passwd;
}

 

Ramanathan Kannabiran

Comments

Comments Nice
Mon. Aug. 14th, 2006 2:46 PM    Scripter ASmith
Comments Wrong requirement
Sat. Oct. 28th, 2006 2:33 PM    Scripter SCoon
Comments Fixed
Sat. Oct. 28th, 2006 2:57 PM    Scripter SCoon
Comments Random Password Generation
Fri. Feb. 17th, 2006 4:29 AM    Beginner ramanathank
Comments Efficiency
Thu. Feb. 16th, 2006 7:27 PM    Helper mercutio
Comments Cleaned up a bit
Thu. Feb. 16th, 2006 4:58 PM    Newbie Olav23
Comments Cool :-)
Thu. Feb. 16th, 2006 11:44 AM    Scripter TimYates

Voting