The principle is very simple, randomly generate one, and then append to the string one by one
Such as:
public class RandomUtil { /** * Generate random numbers of specified digits * @param length * @return */ public static String getRandom(int length){ String val = ""; Random random = new Random(); for (int i = 0; i < length; i++) { val += String.valueOf(random.nextInt(10)); } return val; } public static void main(String[] args) { System.out.println(getRandom(6)); } }