Java generation of random numbers of alphanumeric combinations sample Java generation random number _java

Source: Internet
Author: User

Copy Code code as follows:

Package com.test;

Import Java.util.Random;

public class Generaterandomnumber {

public static void Main (string[] args) {

System.out.println ("Generated 10 is a random number:" + GETCHARANDNUMR (10));
}

/**
* Java generates random numbers and letter combinations
* @param length[Generate random number of lengths]
* @return
*/
public static String getcharandnumr (int length) {
String val = "";
Random Random = new Random ();
for (int i = 0; i < length; i++) {
Output letter or number
String charornum = random.nextint (2)% 2 = 0? "Char": "num";
String
if ("Char". Equalsignorecase (Charornum)) {
Get uppercase or lowercase letters
int choice = random.nextint (2)% 2 = 0? 65:97;
Val + = (char) (Choice + random.nextint (26));
else if ("num". Equalsignorecase (Charornum)) {//number
Val + + string.valueof (Random.nextint (10));
}
}
return Val;
}

}

Related Article

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.