Tool class for generating random numbers, strings in Java
1. Call methods need to pass in the length of the generated string and the type required
Generate random numbers
Generate a random letter string
Generate random strings + numbers, etc.
.........
2. For a total of 8 types, see the comments in the tool class.
1 Importjava.util.Date;2 ImportJava.util.Random;3 ImportJava.util.UUID;4 5 /**6 * @Name Randomstringutil7 * @Descr generate random strings8 * @authorDX9 * @date October 15, 2015 PM 2:36:05Ten */ One Public classRandomstringutil { A /** - * @paramPasslength - *: How many lengths of string to generate the * @paramtype - *: What kind of type is required - * @returnAccording to the type passed in to determine - */ + - //can be based on their own needs to cut down the following code, not the type can be deleted + A //type=0: Pure numerals (0-9) at //type=1: Full lowercase letter (A-Z) - //type=2: All caps (A-Z) - //type=3: Numbers + lowercase letters - //type=4: Numbers + uppercase letters - //type=5: Uppercase + lowercase letters - //type=6: Numbers + uppercase letters + lowercase letters in //type=7: fixed-length 33-bit: Four "-" (equivalent to 33-bit lowercase letters plus numbers) removed from the random string obtained by the UUID - to Public StaticString Getrandomcode (intPasslength,inttype) { +StringBuffer buffer =NULL; -StringBuffer SB =NewStringBuffer (); theRandom r =NewRandom (); *R.setseed (NewDate (). GetTime ()); $ Switch(type) {Panax Notoginseng Case0: -Buffer =NewStringBuffer ("0123456789"); the Break; + Case1: ABuffer =NewStringBuffer ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); the Break; + Case2: -Buffer =NewStringBuffer ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); $ Break; $ Case3: -Buffer =NewStringBuffer ("0123456789abcdefghijklmnopqrstuvwxyz"); - Break; the Case4: -Buffer =NewStringBuffer ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");Wuyi Break; the Case5: -Buffer =NewStringBuffer ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); Wu Break; - Case6: AboutBuffer =NewStringBuffer ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); $Sb.append (Buffer.charat (R.nextint (buffer.length)-10))); -Passlength-= 1; - Break; - Case7: AString s =Uuid.randomuuid (). toString (); +Sb.append (s.substring (0, 8) + s.substring (9) + s.substring (+) + s.substring (+ +) + s.substring (24)); the } - $ if(Type! = 7) { the intRange =buffer.length (); the for(inti = 0; i < passlength; ++i) { the sb.append (Buffer.charat (R.nextint (range))); the } - } in returnsb.tostring (); the } the}
Java random string: Random numeric string, tool class