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

TrandomMember(Collection collection)
random Member
int size = collection.size();
if (size == 0) {
    return null;
int ndx = randomInt(0, size);
int i = 0;
for (T m : collection) {
    if (i == ndx) {
...
longrandomNanoTime()
random Nano Time
long nano = System.nanoTime();
return nano;
StringrandomNick()
random Nick
return "MC" + randomInt(1000, 9999);
StringrandomPassword()
Generate a random password, used when a password validation fails to ensure that the user doesn't get saved with some sort of invalid (and thus insecure) password.
String methodName = MODULE_NAME + "randomPassword()";
String retval = "";
char[] pwd = new char[12];
Random randomGuy = new Random();
for (int i = 0; i < pwd.length; i++) {
    pwd[i] = (char) (randomGuy.nextInt(86) + 40);
retval = new String(pwd);
...
int[]randomPermutation(int size)
Credit to mikio braun from jblas

Create a random permutation of the numbers 0, ..., size - 1.

Random r = new Random();
int[] result = new int[size];
for (int j = 0; j < size; j++) {
    result[j] = j + 1;
for (int j = size - 1; j > 0; j--) {
    int k = r.nextInt(j);
    int temp = result[j];
...
int[]randomPermutations(int[] tab, Random r)
random Permutations
int l = tab.length;
for (int i = 0; i < l; i++) {
    int j = r.nextInt(l);
    int tmp = tab[i];
    tab[i] = tab[j];
    tab[j] = tmp;
return tab;
...
voidrandomPermute(List l, Random rand)
random Permute
for (int i = 0; i < l.size(); i++) {
    int j = i + rand.nextInt(l.size() - i);
    T x = l.get(i);
    l.set(i, l.get(j));
    l.set(j, x);
booleanrandomProbability(double probability)
{ method randomProbability returns true with probabiliy given by its input
if (probability > 1 || probability < 0)
    throw new IllegalArgumentException("randomProbability must take a 0 <= number <= 1");
if (probability < 0)
    throw new IllegalArgumentException("randomProbability must take a number > 0");
double test = gRandomizer.nextDouble();
return (test < probability);
StringrandomPseudo()
random Pseudo
final int length = random(4, 8);
StringBuilder sb = new StringBuilder(length);
boolean flag = random(0, 1) == 1;
for (int i = 0; i < length; ++i) {
    sb.append(flag ? random(VOWELS) : random(CONSONANTS));
    flag = !flag;
return sb.toString();
...
int[]randomRange(int end)
random Range
return randomRange(end, System.currentTimeMillis());