Java Utililty Methods Random Int

List of utility methods to do Random Int

Description

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

Method

intgenerateRandomInt(final int max)
generate Random Int
return (int) (Math.random() * max);
intgenerateRandomInt(int min, int max)
generate Random Int
Random random = new Random();
return random.nextInt(max) + min;
intgenerateRandomInteger(int aStart, int aEnd, Random aRandom)
Erzeugt eine Zufallszahl
if (aStart > aEnd) {
    throw new IllegalArgumentException("Start cannot exceed End.");
long range = (long) aEnd - (long) aStart + 1;
long fraction = (long) (range * aRandom.nextDouble());
int randomNumber = (int) (fraction + aStart);
return randomNumber;
intgenerateRandomInteger(int start, int end)
Generates a random integer in the interval [start, end] (interval includes both start and end) and returns it.
int range = end - start;
if (_randomNumberGenerator == null)
    _randomNumberGenerator = new Random();
if (range > 0)
    return _randomNumberGenerator.nextInt(range) + start;
return start;
Integer[]generateRandomIntegerArray(int size, int min, int max)
generate Random Integer Array
Random random = new Random(System.currentTimeMillis());
Integer array[] = new Integer[size];
for (int i = 0; i < size; i++) {
    array[i] = random.nextInt(max - min + 1) + min;
return array;
SetgenerateRandomIntegers(int numbersNeeded, int bound)
generate Random Integers
if (numbersNeeded > bound) {
    throw new RuntimeException("Can not generate number more than bound");
Random rng = new Random();
Set<Integer> generated = new LinkedHashSet<Integer>();
while (generated.size() < numbersNeeded) {
    Integer next = rng.nextInt(bound);
    generated.add(next);
...
intgenerateRandomIntInRange(int min, int max)
generate Random Int In Range
return min + (int) (Math.random() * ((max - min) + 1));
Integer[]generateRandomIntObj(int range, int noOfElements)
generate Random Int Obj
int arr[] = generateRandomInts(range, noOfElements);
Integer generatedInts[] = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) {
    generatedInts[i] = arr[i];
return generatedInts;
int[]generateRandomInts(int range, int noOfElements)
generate Random Ints
Random randomGenerator = new Random();
int generatedInts[] = new int[noOfElements];
for (int i = 0; i < generatedInts.length; i++) {
    generatedInts[i] = randomGenerator.nextInt(range);
return generatedInts;
intgenerateRandomIntUpto(int max)
generate Random Int Upto
return nextInt(max);