Commonly used random string Retrieval

Source: Internet
Author: User

Package com. tianren. util;
Import java. util. Random;

/**
* Randomstring. Java
* Utils class
* @ Author jinxx
* @ Version 1.0
**/
Public class randomstring {

/** Returns an uppercase random letter string */
Public static string getrandomstringu (INT length ){
String STR = "abcdefghijklmnopqrstuvwxyz ";
Random random = new random ();
Stringbuffer sb = new stringbuffer ();
For (int I = 0; I <length; ++ I ){
Int number = random. nextInt (26 );
Sb. append (str. charAt (number ));
}
Return sb. toString ();
}

/** Returns a lowercase random letter string */
Public static String getRandomStringL (int length ){
String str = "abcdefghijklmnopqrstuvwxyz ";
Random random = new random ();
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <length; ++ I ){
Int number = random. nextint (26 );
SB. append (Str. charat (number ));
}
Return sb. tostring ();
}

/** Returns a random number string [0-9] */
Public static string getrandomstringn10 (INT length ){
String STR = "0123456789 ";
Random random = new random ();
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <length; ++ I ){
Int number = random. nextint (10 );
SB. append (Str. charat (number ));
}
Return sb. tostring ();
}

/** Returns a random number string [1-9] */
Public static string getrandomstring) (INT length ){
String STR = "123456789 ";
Random random = new random ();
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <length; ++ I ){
Int number = random. nextint (9 );
SB. append (Str. charat (number ));
}
Return sb. toString ();
}

/** Return a random number [1-9] */
Public static int getRandomIntN9 (int length ){
String str = "123456789 ";
Random random = new Random ();
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <length; ++ I ){
Int number = random. nextInt (9 );
SB. append (Str. charat (number ));
}
Return integer. parseint (sb. tostring ());
}

/** Return a random number [0-9] */
Public static int getrandomintn10 (INT length ){
String STR = "0123456789 ";
Random random = new random ();
Stringbuffer sb = new stringbuffer ();
For (int I = 0; I <length; ++ I ){
Int number = random. nextInt (10 );
Sb. append (str. charAt (number ));
}
Return Integer. parseInt (sb. toString ());
}

/** Returns a string consisting of uppercase and lowercase letters and digits */
Public static String getRandomStringA (int length ){
Random random = new Random ();
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <length; ++ I ){
Int number = random. nextInt (3 );
Long result = 0;
Switch (number ){
Case 0:
Result = Math. round (Math. random () * 25 + 64 );
Sb. append (String. valueOf (char) result ));
Break;
Case 1:
Result = Math. round (Math. random () * 25 + 97 );
Sb. append (String. valueOf (char) result ));
Break;
Case 2:
Sb. append (String. valueOf (new Random (). nextInt (10 )));
Break;
}
}
Return sb. 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.