Original: http://www.jb51.net/article/45006.htm
PackageP2p_web;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Random;/*** Character randomly generated class *@authorASUS **/ Public classPassWord {/*** Password Type enumeration *@authorASUS*/ Public Static enumTYPE {/*** Character Type*/Letter ,/*** Uppercase and lowercase 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 + number + symbol type*/Letter_capital_number_sign}Private Staticstring[] 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 Staticstring[] 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 Staticstring[] Number = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}; Private StaticString[] Sign = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "'", "-", "=", "{", "}", "|", ":", "\" "," < "," > ","? ", "[","]","\\",";","‘",",",".","/"}; /*** Static random number*/ Private StaticRandom random =NewRandom (); Public Static voidMain (string[] args) {System.out.println (Password.getrandom (24, PassWord.TYPE.LETTER_CAPITAL_NUMBER_SIGN)); } /*** Get Random Combo code *@paramnum Number of digits *@paramtype * @type * <br> character letter, * <br> uppercase Capital, * <br> Numeric number, * <br> symbol sign , * <br> large + small character type letter_capital, * <br> small character + digital Letter_number, * <br> large + tiny character + digital Letter_capital_number, * <br> Large + small character + number + symbol type Letter_capital_number_sign*/ Public StaticString Getrandom (intNum,type TYPE) {ArrayList<String> temp =NewArraylist<string>(); StringBuffer Code=NewStringBuffer (); if(Type = =TYPE. Letter) {Temp.addall (arrays.aslist (lowercase)); }Else if(Type = =TYPE. Capital) {Temp.addall (arrays.aslist); }Else if(Type = =TYPE. Number) {Temp.addall (arrays.aslist (number)); }Else if(Type = =TYPE. Sign) {Temp.addall (arrays.aslist); }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(inti = 0; i < num; i++) {code.append (Temp.get (Random.nextint (Temp.size ()))); } returncode.tostring ();}}
Java randomly generated strings (character randomly generated classes generate random character combinations)