Java method for generating random passwords (random length, specified length)

Source: Internet
Author: User
Tags abs stub stringbuffer

Java generates a random length password.

The code is as follows: Copy code

/**
* Generate a random password
*
* @ Param pwd_len
* Total length of the generated password
* @ Return password string
*/
Public static String genRandomNum (int pwd_len ){
// 35 is because the array starts from 0 and 26 letters + 10 digits
Final int maxNum = 36;
Int I; // The random number generated
Int count = 0; // The length of the generated password
Char [] str = {'a', 'B', 'c', 'D', 'e', 'F', 'G', 'H ', 'I', 'J', 'K ',
'L', 'M', 'n', 'O', 'P', 'Q', 'R', 'S', 'T', 'u ', 'V', 'W ',
'X', 'y', 'z', '0 & prime;, '1 & prime;, '2 & prime;, '3 & prime ;, '4 & prime;, '5 & prime;, '6 & prime;, '7 & prime;, '8 & prime;, '9 & prime ;};

StringBuffer pwd = new StringBuffer ("");
Random r = new Random ();
While (count <pwd_len ){
// Generate a random number and take the absolute value to prevent negative numbers,

I = Math. abs (r. nextInt (maxNum); // The maximum number generated is 36-1.

If (I> = 0 & I <str. length ){
Pwd. append (str [I]);
Count ++;
}
}

Return pwd. toString ();
}

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. println (genRandomNum (14); // Generate a 14-bit random password
}


Generate a random password of the specified length

The code is as follows: Copy code


Import java. util. Random;

Public class test {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
System. out. println (genRandomNum (10 ));
 }
/**
* Generate a password immediately
* @ Param pwd_len total length of the password generated
* @ Return password string
*/
Public static String genRandomNum (int pwd_len ){
// 35 is because the array starts from 0 and 26 letters + 10 digits
Final int maxNum = 36;
Int I; // The random number generated
Int count = 0; // The length of the generated password
Char [] str = {'a', 'B', 'c', 'D', 'e', 'F', 'G', 'H ', 'I', 'J', 'K ',
'L', 'M', 'n', 'O', 'P', 'Q', 'R', 'S', 'T', 'u ', 'V', 'W ',
'X', 'y', 'z', '0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9 '};
 
StringBuffer pwd = new StringBuffer ("");
Random r = new Random ();
While (count <pwd_len ){
// Generate a random number and take the absolute value to prevent negative numbers,
  
I = Math. abs (r. nextInt (maxNum); // The maximum number generated is 36-1.
  
If (I> = 0 & I <str. length ){
Pwd. append (str [I]);
Count ++;
   }
  }
 
Return pwd. toString ();
 }
}

Java generates 15-bit random code

The code is as follows: Copy code

Import java. util. ArrayList;
Import java. util. List;

Public class GeneratePswd {

@ SuppressWarnings ({"unchecked", "unchecked "})
Public static void main (String [] args ){

StringBuffer buf = new StringBuffer ("a, B, c, d, e, f, g, h, I, g, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z ");
Buf. append (", A, B, C, D, E, F, G, H, I, G, K, L, M, N, O, P, Q, R, s, T, U, V, W, X, Y, Z ");
Buf. append (",~, @, #, $, %, ^, &, *, (,), _, +, | ,',.");
Buf. append (", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ");

String [] arr = buf. toString (). split (",");
List pswdList = new ArrayList ();
  
For (int I = 0; I <50; I ++ ){
PswdList. add (getPswd (arr ));
  }
  
For (int I = 0; I <pswdList. size (); I ++ ){
System. out. println (pswdList. get (I ));
  }
 }
 
Public static String getPswd (String [] arr ){
StringBuffer B = new StringBuffer ();
Java. util. Random r;
Int k;
For (int I = 0; I <15; I ++ ){
R = new java. util. Random ();
K = r. nextInt ();
B. append (String. valueOf (arr [Math. abs (k % 76)]);
  }
  
Return B. toString ();
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.