Java Random Number getRndNumByLen(int lengthOfNumber)

Here you can find the source of getRndNumByLen(int lengthOfNumber)

Description

generate specified length string with numbers.

License

Apache License

Parameter

Parameter Description
lengthOfNumber the length of the number string to be created.

Declaration

public static String getRndNumByLen(int lengthOfNumber) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    /**//  ww  w  .  j  av  a 2 s  . co  m
     * generate specified length string with numbers.
     * 
     * @param lengthOfNumber
     *            the length of the number string to be created.
     */
    public static String getRndNumByLen(int lengthOfNumber) {
        int i, count = 0;

        StringBuffer randomStr = new StringBuffer("");
        Random rnd = new Random();

        while (count < lengthOfNumber) {
            i = Math.abs(rnd.nextInt(9));
            if (i == 0 && count == 0) {
            } else {
                randomStr.append(String.valueOf(i));
                count++;
            }
        }
        return randomStr.toString();
    }
}

Related

  1. getRandomNumer(int length)
  2. getRandomNumericString(int maxlen)
  3. getRandomPortNumber()
  4. getRandonNumber(int range)
  5. getrannumber()
  6. getSerialNumber(int length)
  7. getTenByteNumber()
  8. randomHexNumber(final int length)
  9. randomNumber(int length)