1. Obtain a random string in the range of 0-9, az, AZ
Copy the code code as follows:
/**
* JAVA obtains random numbers in the range of 0-9, az, AZ
* @param length random number length
* @return String
*/
public static String getRandomChar(int length) {
char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm ', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M ', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(chr[random.nextInt(62)]);
}
return buffer.toString();
}
public static String getRandomChar() {
return getRandomChar(10);
}
2. Obtain random numbers from 0-9
Copy the code code as follows:
/**
* JAVA gets the random number length from 0 to 9, the default length is 10
*
* @return String
*/
public static String getRandomNumber() {
return getRandomNumber(10);
}
3. Another implementation of JAVA to obtain random numbers from 0 to 9
Copy the code code as follows:
/**
* JAVA gets a random number from 0-9
*
* @param length
* @return String
*/
public static String getRandomNumber(int length) {
Random random = new Random();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.append(random.nextInt(10));
}
return buffer.toString();
}