Java Random Number genRandomNumberString(int numberCount)

Here you can find the source of genRandomNumberString(int numberCount)

Description

gen Random Number String

License

Open Source License

Declaration

public static String genRandomNumberString(int numberCount) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    private static final char[] DEFAULT_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
            'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', };
    private static final char[] NUMBER_CHARS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', };
    private static final char[] NONEZERO_NUMBER_CHARS = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', };

    public static String genRandomNumberString(int numberCount) {
        return genRandomString(1, NONEZERO_NUMBER_CHARS) + genRandomString(numberCount - 1, NUMBER_CHARS);
    }//  w w  w  . j  a v a 2s  .c  o  m

    public static String genRandomString(int count) {
        return genRandomString(count, DEFAULT_CHARS);
    }

    public static String genRandomString(int count, char[] elements) {
        StringBuffer sb = new StringBuffer();

        int len = elements.length - 1;
        for (int i = 0; i < count; i++) {
            sb.append(elements[new Random(System.nanoTime()).nextInt(len)]);
        }

        return sb.toString();
    }
}

Related

  1. genRandomNum(int pwd_len)
  2. genRandomNumber()
  3. genRandomNumber(int begin, int end)
  4. genRandomNumber(int digitNumber)
  5. genRandomNumber(int minNumber, int maxNumber, int digits)
  6. genRandomNumList(int num, int count)
  7. getChance(Number number)
  8. getDocumentNumber()
  9. getFlightNumber()