Java List Random Item getRandomElement(final List list)

Here you can find the source of getRandomElement(final List list)

Description

Returns a random element from the specified container.

License

MIT License

Parameter

Parameter Description
list the specified list

Declaration

public static <T> T getRandomElement(final List<T> list) 

Method Source Code

//package com.java2s;
// BWEM is free software, licensed under the MIT/X11 License. 

import java.util.List;
import java.util.SplittableRandom;

public class Main {
    private static final SplittableRandom randomGenerator = new SplittableRandom();

    /**// w w  w .ja v  a2s  .  co m
     * Returns a random element from the specified container.
     * There is nothing special about this function other than to provide
     * a function similar to the one provided in the original C++ BWEM.
     *
     * @param list the specified list
     */
    public static <T> T getRandomElement(final List<T> list) {
        final int randomIndex = randomGenerator.nextInt(list.size());
        return list.get(randomIndex);
    }
}

Related

  1. chooseRandom(List list)
  2. chooseRandomElement(List list, Random random)
  3. computeCrossProduct(List> allArgPossibilities, int maximumSize)
  4. findFirst(List list, T value, Comparator comparator)
  5. generateInteger(int min, int maxInclusive, List excludedValues)
  6. getRandomElement(List myList)
  7. getRandomElementFromList(List l)
  8. getRandomFromList(List list)
  9. getRandomIdxList(int minIdx, int maxIdx)