1 PackageCom.zuidaima.core.util;2 3 ImportJava.util.Random;4 5 Public classRandomutil {6 Public Static FinalString Allchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";7 Public Static FinalString Letterchar = "ABCDEFGHIJKLLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";8 Public Static FinalString Numberchar = "0123456789";9 Ten /** One * Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers) A * - * @paramlength - * Random string length the * @returnRandom String - */ - Public StaticString generatestring (intlength) { -StringBuffer SB =NewStringBuffer (); +Random random =NewRandom (); - for(inti = 0; i < length; i++) { + sb.append (Allchar.charat (Random.nextint (Allchar.length () )); A } at returnsb.tostring (); - } - - /** - * Returns a fixed-length random plain letter string (with uppercase and lowercase letters only) - * in * @paramlength - * Random string length to * @returnRandom String + */ - Public StaticString generatemixstring (intlength) { theStringBuffer SB =NewStringBuffer (); *Random random =NewRandom (); $ for(inti = 0; i < length; i++) {Panax Notoginseng sb.append (Allchar.charat (Random.nextint (Letterchar.length () )); - } the returnsb.tostring (); + } A the /** + * Returns a fixed-length random, purely uppercase string (with uppercase and lowercase letters only) - * $ * @paramlength $ * Random string length - * @returnRandom String - */ the Public StaticString generatelowerstring (intlength) { - returngeneratemixstring (length). toLowerCase ();Wuyi } the - /** Wu * Returns a fixed-length random, plain lowercase letter string (only uppercase letters) - * About * @paramlength $ * Random string length - * @returnRandom String - */ - Public StaticString generateupperstring (intlength) { A returngeneratemixstring (length). toUpperCase (); + } the - /** $ * Generate a fixed-length pure 0 string the * the * @paramlength the * String Length the * @returnPure 0 String - */ in Public StaticString generatezerostring (intlength) { theStringBuffer SB =NewStringBuffer (); the for(inti = 0; i < length; i++) { AboutSb.append (' 0 '); the } the returnsb.tostring (); the } + - /** the * Generate a fixed length string according to the number, the length is not enough for the previous 0Bayi * the * @paramNum the * Digital - * @paramFixdlenth - * String Length the * @returnfixed-length string the */ the Public StaticString tofixdlengthstring (LongNumintfixdlenth) { theStringBuffer SB =NewStringBuffer (); -String Strnum =string.valueof (num); the if(Fixdlenth-strnum.length () >= 0) { theSb.append (generatezerostring (Fixdlenth-strnum.length ())); the}Else {94 Throw NewRuntimeException ("Convert number + num +" to length "+Fixdlenth the+ "The string has an exception! "); the } the sb.append (strnum);98 returnsb.tostring (); About } - 101 /**102 * The number of Len bits generated each time is different103 * 104 * @paramparam the * @returnfixed-length numbers106 */107 Public Static intGetnotsimple (int[] param,intLen) {108Random Rand =NewRandom ();109 for(inti = param.length; i > 1; i--) { the intindex =Rand.nextint (i);111 intTMP =Param[index]; theParam[index] = param[i-1];113PARAM[I-1] =tmp; the } the intresult = 0; the for(inti = 0; i < Len; i++) {117result = result * 10 +Param[i];118 }119 returnresult; - }121 122 Public Static voidMain (string[] args) {123System.out.println ("Returns a fixed-length random string containing only uppercase and lowercase letters, numbers):" + generatestring (10));124 System.out the. println ("Returns a fixed-length random, pure-letter string containing only uppercase and lowercase letters):" + generatemixstring (10));126System.out.println ("Returns a fixed-length random, purely uppercase string (with uppercase and lowercase letters only):"127+ generatelowerstring (10)); -System.out.println ("Returns a fixed-length random, plain lowercase letter string containing only uppercase letters):"129+ generateupperstring (10)); theSYSTEM.OUT.PRINTLN ("Generates a fixed-length pure 0 string:" + generatezerostring (10));131SYSTEM.OUT.PRINTLN ("Generates a fixed-length string based on a number that is not longer than the previous 0:" the+ tofixdlengthstring (123, 10));133 int[] in = {1, 2, 3, 4, 5, 6, 7 };134System.out.println ("The number of Len bits generated each time is different:" + getnotsimple (in, 3));135 }136}
The most complete Java random number generation algorithm in history [reprint]