Java Utililty Methods Random

List of utility methods to do Random

Description

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

Method

doublerandomInRange(double min, double max)
random In Range
double range = max - min;
double scaled = random.nextDouble() * range;
return scaled + min;
IteratorrandomInsults()
random Insults
return randomInsults(42);
int[]randomIntArray(int len, Random rand)
random Int Array
int[] out = new int[len];
for (int i = 0; i < len; i++) {
    out[i] = rand.nextInt();
    if (out[i] < 0)
        out[i] = -out[i];
return out;
ListrandomIntegerList(int sz, int min, int max)
random Integer List
List<Integer> lst = new ArrayList<>(sz);
for (int i = 0; i < sz; i++)
    lst.add(randInt(min, max));
return lst;
ListrandomIntegers(int sz)
random Integers
List<Integer> lst = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < sz; i++) {
    lst.add(rand.nextInt(sz * 10));
return lst;
IterablerandomIterable(Collection col)
Takes given collection, shuffles it and returns iterable instance.
List<T> list = new ArrayList<>(col);
Collections.shuffle(list);
return list;
StringrandomKey()
random Key
byte[] key = new byte[16];
for (int i = 0; i < 16; i++) {
    key[i] = (byte) (-128 + (int) (Math.random() * ((127 - -128) + 1)));
return new String(key);
StringrandomKey()
Method to generate random key
return Long.toHexString(Double.doubleToLongBits(Math.random()));
charrandomLetter()
random Letter
return letters[rand.nextInt(letters.length)];
ListrandomList(Collection collection)
random List
List<E> list = new ArrayList<>(collection);
Collections.shuffle(list);
return list;