Copy the code code as follows:
package com.test;
import java.util.Random;
public class GenerateRandomNumber {
public static void main(String[] args) {
System.out.println("The generated 10 random numbers are: " + getCharAndNumr(10));
}
/**
* java generates random number and letter combinations
* @param length[the length of the generated random number]
* @return
*/
public static String getCharAndNumr(int length) {
String val = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
// Output letters or numbers
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// string
if ("char".equalsIgnoreCase(charOrNum)) {
// Get uppercase letters 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;
}
}