java generates random number with specified number of digits

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));
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972474&siteId=291194637