Java Utililty Methods Random Number

List of utility methods to do Random Number

Description

The list of methods to do Random Number are organized into topic(s).

Method

StringgetRandomNumber(int randomCount)
get Random Number
Random random = new Random();
StringBuilder sb = new StringBuilder(randomCount);
for (int i = 0; i < randomCount; i++)
    sb.append((char) ('0' + random.nextInt(10)));
return sb.toString();
intgetRandomNumber(int startNumber, int endNumber)
get Random Number
Random randomGenerator = new Random();
long range = (long) startNumber - (long) endNumber + 1;
long fraction = (long) (range * randomGenerator.nextDouble());
int randomNumber = (int) (fraction + startNumber);
return (Math.abs(randomNumber) == 0) ? getRandomNumber(startNumber, endNumber) : Math.abs(randomNumber);
intgetRandomNumberBetween(int min, int max)
Returns a random int between min and max, inclusive.
return useRandomForNumberBetween(rand, min, max);
longgetRandomNumberFixLength()
get Random Number Fix Length
return 1000000000L + generator.nextInt(2000000000);
RandomgetRandomNumberGenerator()
get Random Number Generator
return new Random(++seedQualifier + System.nanoTime());
intgetRandomNumberInRange(Random random, int min, int max)
Returns a random number between min and max, inclusive.
return min + random.nextInt(max - min + 1);
ArrayListgetRandomNumbers(int range, int count, Random rnd)
get Random Numbers
if (count > range) {
    return null;
if (count < 0 || range < 0) {
    return null;
ArrayList<Integer> indices = new ArrayList<Integer>();
if (count < range / 2) {
...
StringgetRandomNumberString(int length)
get Random Number String
Random rand = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
    int pos = rand.nextInt(NUMBERS.length());
    sb.append(NUMBERS.charAt(pos));
return sb.toString();
StringgetRandomNumer(int length)
get Random Numer
if (length > 9 || length < 1) {
    return null;
int maxNum = (int) Math.pow(10, length);
Random random = new Random(System.currentTimeMillis());
int code = random.nextInt(maxNum - 1);
return String.format("%0" + length + "d", code);
StringgetRandomNumericString(int maxlen)
get Random Numeric String
String str = new String();
for (int j = 0, n = R.nextInt(maxlen) + 1; j < n; ++j) {
    str += (char) ('0' + R.nextInt(10));
return str;