Java random generation string (character randomly generated class generates random character combination) _java

Source: Internet
Author: User
Tags addall uppercase character stringbuffer

Copy Code code as follows:

Package Com.phyl.password;

Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.Random;
/**
* Character Random Generation class
* @author ASUS
*
*/
public class PassWord {

/**
* Password Type Enumeration
* @author ASUS
*/
public static enum TYPE {
/**
* Character type
*/
Letter,
/**
* Uppercase Character type
*/
Capital,
/**
* Digital Type
*/
Number,
/**
* Symbol type
*/
SIGN,
/**
* Large + small character type
*/
Letter_capital,
/**
* Small character + digital type
*/
Letter_number,
/**
* Large + small character + digital type
*/
Letter_capital_number,
/**
* Large + small character + digital + symbol type
*/
Letter_capital_number_sign
}

private static string[] lowercase = {
"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"};

private static string[] Capital = {
"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"};

private static string[] Number = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

private static string[] Sign = {
"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "'", "-", "=",
"{", "}", "|", ":", "\" "," < "," > ","? ",
"[","]","\\",";","'",",",".","/"};

/**
* Static random number
*/
private static Random Random = new Random ();

public static void Main (string[] args) {
System.out.println (Password.getrandom, PassWord.TYPE.CAPITAL));
}

/**
* Get random combination code
* @param num Digits
* @param type
* @type
* <br> Letter of character,
* <br> uppercase character type capital,
* <br> Number Type,
* <br> symbol Type SIGN,
* <br> large + small character type letter_capital,
* <br> Small character + digital type Letter_number,
* <br> Large + small character + digital type Letter_capital_number,
* <br> Large + small character + digital + symbol type Letter_capital_number_sign
*/
public static String getrandom (int num,type TYPE) {
arraylist<string> temp = new arraylist<string> ();
StringBuffer code = new StringBuffer ();
if (type = = Type. Letter) {
Temp.addall (arrays.aslist (lowercase));
}else if (type = = Type. Capital) {
Temp.addall (arrays.aslist (capital));
}else if (type = = Type. Number) {
Temp.addall (arrays.aslist (number));
}else if (type = = Type. SIGN) {
Temp.addall (Arrays.aslist (sign));
}else if (type = = Type. Letter_capital) {
Temp.addall (arrays.aslist (lowercase));
Temp.addall (arrays.aslist (capital));
}else if (type = = Type. Letter_number) {
Temp.addall (arrays.aslist (lowercase));
Temp.addall (arrays.aslist (number));
}else if (type = = Type. Letter_capital_number) {
Temp.addall (arrays.aslist (lowercase));
Temp.addall (arrays.aslist (capital));
Temp.addall (arrays.aslist (number));
}else if (type = = Type. Letter_capital_number_sign) {
Temp.addall (arrays.aslist (lowercase));
Temp.addall (arrays.aslist (capital));
Temp.addall (arrays.aslist (number));
Temp.addall (Arrays.aslist (sign));
}
for (int i = 0; i < num; i++) {
Code.append (Temp.get (Random.nextint (Temp.size ()));
}
return code.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.