Here you can find the source of getRandomNumber(int length)
public static String getRandomNumber(int length)
//package com.java2s; //License from project: Apache License import java.util.Random; public class Main { public static String getRandomNumber(int length) { StringBuffer bu = new StringBuffer(); String[] arr = { "2", "3", "4", "5", "6", "7", "8", "9", "1", "0" }; Random random = new Random(); while (bu.length() < length) { String temp = arr[random.nextInt(10)]; if (bu.indexOf(temp) == -1) { bu.append(temp);// w w w . j av a2 s . c o m } } return bu.toString(); } public static int indexOf(String str, char strChar) { if (isEmpty(str)) { return -1; } return str.indexOf(strChar); } /** * <pre> * ExmayStringUtils.indexOf(null, *) = -1 * ExmayStringUtils.indexOf(*, null) = -1 * ExmayStringUtils.indexOf("", "") = 0 * ExmayStringUtils.indexOf("aabaabaa", "a") = 0 * ExmayStringUtils.indexOf("aabaabaa", "b") = 2 * ExmayStringUtils.indexOf("aabaabaa", "ab") = 1 * ExmayStringUtils.indexOf("aabaabaa", "") = 0 * </pre> * * @param str * @param searchStr * @return */ public static int indexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.indexOf(searchStr); } public static int indexOf(String str, char searchChar, int startPos) { if (isEmpty(str)) { return -1; } return str.indexOf(searchChar, startPos); } public static boolean isEmpty(String str) { return str == null || str.length() == 0 || str.equals("") || str.matches("\\s*"); } }