Java tutorial
//package com.java2s; import java.util.Random; public class Main { static Random r = new Random(); public static String randomCode(int length) { int seed = Double.valueOf(Math.pow(10, length)).intValue(); return String.format("%0" + length + "d", (r.nextInt(seed))); } }