Here you can find the source of getRandomNumer(int length)
public static String getRandomNumer(int length)
//package com.java2s; import java.util.Random; public class Main { public static String getRandomNumer(int length) { if (length > 9 || length < 1) { return null; }// w w w . ja v a 2 s . c o m int maxNum = (int) Math.pow(10, length); Random random = new Random(System.currentTimeMillis()); int code = random.nextInt(maxNum - 1); return String.format("%0" + length + "d", code); } }