Random Verification Code Generation code

Source: Internet
Author: User

Random Verification Code Generation code
PackageCom.zuidaima.core.util; ImportJava.util.Random; Public classRandomutil { Public Static FinalString Allchar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Public Static FinalString Letterchar = "ABCDEFGHIJKLLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"; Public Static FinalString Numberchar = "0123456789"; /*** Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers) * *@paramlength * Random string lengths *@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 ()))); } returnsb.tostring (); } /*** Returns a fixed-length random plain letter string (with uppercase and lowercase letters only) * *@paramlength * Random string lengths *@returnRandom String*/ Public StaticString generatemixstring (intlength) {StringBuffer SB=NewStringBuffer (); Random Random=NewRandom (); for(inti = 0; i < length; i++) {sb.append (Allchar.charat (Random.nextint (Letterchar.length ()))); } returnsb.tostring (); } /*** Returns a fixed-length random pure capital letter string (with uppercase and lowercase letters only) * *@paramlength * Random string lengths *@returnRandom String*/ Public StaticString generatelowerstring (intlength) { returngeneratemixstring (length). toLowerCase (); } /*** Returns a fixed-length random plain lowercase letter string (with only uppercase letters) * *@paramlength * Random string lengths *@returnRandom String*/ Public StaticString generateupperstring (intlength) { returngeneratemixstring (length). toUpperCase (); } /*** Generate a fixed-length pure 0 String * *@paramLength * String lengths *@returnPure 0 String*/ Public StaticString generatezerostring (intlength) {StringBuffer SB=NewStringBuffer (); for(inti = 0; i < length; i++) {sb.append (' 0 '); } returnsb.tostring (); } /*** Generate a fixed-length string based on the number, the length is not enough for the front 0 * *@paramnum * Number *@paramFixdlenth * String length *@returnfixed-length string*/ Public StaticString tofixdlengthstring (LongNumintfixdlenth) {StringBuffer SB=NewStringBuffer (); String Strnum=string.valueof (num); if(Fixdlenth-strnum.length () >= 0) {sb.append (generatezerostring (Fixdlenth-strnum.length ())); } Else { Throw NewRuntimeException ("Convert number + num +" to length "+Fixdlenth+ "The string has an exception! "); } sb.append (Strnum); returnsb.tostring (); } /*** The number of Len bits generated each time is different * *@paramparam *@returnfixed-length numbers*/ Public Static intGetnotsimple (int[] param,intLen) {Random Rand=NewRandom (); for(inti = param.length; i > 1; i--) { intindex =Rand.nextint (i); intTMP =Param[index]; Param[index]= Param[i-1]; Param[i-1] =tmp; } intresult = 0; for(inti = 0; i < Len; i++) {result= result * 10 +Param[i]; } returnresult; } Public Static voidMain (string[] args) {System.out.println ("Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers):" + generatestring (10)); System.out. println ("Returns a fixed-length random, pure-letter string (with uppercase and lowercase letters only):" + generatemixstring (10)); System.out.println ("Returns a fixed-length random, purely uppercase string (with uppercase and lowercase letters only):" + generatelowerstring (10)); System.out.println ("Returns a fixed-length random plain lowercase letter string (with only uppercase letters):" + generateupperstring (10)); System.out.println ("Generate a fixed-length pure 0 string:" + generatezerostring (10)); System.out.println ("Generates a fixed-length string based on a number that is not longer than the previous 0:" + tofixdlengthstring (123, 10)); int[] in = {1, 2, 3, 4, 5, 6, 7 }; System.out.println ("The number of Len bits generated each time is different:" + getnotsimple (in, 3)); } }

Random Captcha Code generation (RPM)

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.