Java Utililty Methods Random Element

List of utility methods to do Random Element

Description

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

Method

TrandomEle(List list)
random Ele
return randomEle(list, list.size());
ByterandomElem(Random rand, Set partitions)
random Elem
int item = rand.nextInt(partitions.size());
int i = 0;
for (Byte obj : partitions) {
    if (i == item)
        return obj;
    i = i + 1;
return partitions.iterator().next();
...
ErandomElement(Collection coll)
random Element
return randomElement(coll, new Random());
ErandomElement(E[] array)
random Element
return array[randomPositiveInt() % array.length];
ErandomElement(final Set set)
random Element
final int size = set.size();
final int index = RANDOM_GENERATOR.nextInt(size);
int i = 0;
for (final E element : set) {
    if (i == index) {
        return element;
    i++;
...
intrandomElement(int[] anArray)
random Element
if (anArray.length == 0) {
    return 0;
return anArray[nextInt(anArray.length)];
TrandomElement(List list)
Selects a random element from a list.
if (list.isEmpty())
    throw new IllegalArgumentException("Can't select random element from an empty list.");
return list.get(random.get().nextInt(list.size()));
TrandomElement(T... values)
random Element
if (values.length == 0)
    throw new IllegalArgumentException("Cannot choose random value from an empty array");
return values[random.nextInt(values.length)];
TrandomElement(T[] array)
Picks a random element out of any array type.
return array[(int) (RANDOM.nextDouble() * array.length)];
TrandomElementOf(final List list)
Get a random element from the list.
if (list.isEmpty()) {
    return null;
return list.get(nextInt(0, list.size()));