Java Random Number getRandomNumber(int length)

Here you can find the source of getRandomNumber(int length)

Description

get Random Number

License

Apache License

Declaration

public static String getRandomNumber(int length) 

Method Source Code

//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*");
    }
}

Related

  1. getRandomNumber(int bit)
  2. getRandomNumber(int bound)
  3. getRandomNumber(int digit)
  4. getRandomNumber(int len)
  5. getRandomNumber(int len)
  6. getRandomNumber(int length)
  7. getRandomNumber(int length)
  8. getRandomNumber(int max, boolean include0)
  9. getRandomNumber(int max, int count)