Java Utililty Methods List Random Item

List of utility methods to do List Random Item

Description

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

Method

TgetRandomElement(List myList)
Get a random element from a list.
return myList.get(getRandomOffset(myList.size()));
TgetRandomElementFromList(List l)
get Random Element From List
return l.get((int) (Math.random() * l.size()));
TgetRandomFromList(List list)
get Random From List
int position = (int) (Math.random() * (list.size() - 1));
return list.get(position);
ListgetRandomIdxList(int minIdx, int maxIdx)
get Random Idx List
List<Integer> randomIdxList = new ArrayList<Integer>();
for (int i = minIdx; i <= maxIdx; i++)
    randomIdxList.add(i);
return randomIdxList;
intgetRandomIntBetween(int min, int max, List excludeList)
get Random Int Between
int value = getRandomInt(max, false);
while (value < min || excludeList.contains(value)) {
    value = getRandomInt(max, false);
return value;
TlistChooseOne(List list)
list Choose One
if (list.size() == 1) {
    return list.get(0);
if (list.size() > 1) {
    return list.get(R.nextInt(list.size()));
return null;
ListmakeListOfIntegersInRange(Integer size, Integer maxOfRange)
make List Of Integers In Range
List<Integer> returnVal = new ArrayList<>();
Random random = new Random(27350);
while (returnVal.size() < size) {
    Integer currentVal = random.nextInt(maxOfRange);
    if (!returnVal.contains(currentVal)) {
        returnVal.add(currentVal);
return returnVal;
ArrayListmakeRandomList(int arraySize, boolean unique)
make Random List
int[] values = makeRandomArray(arraySize, unique);
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < values.length; i++) {
    result.add(values[i]);
return result;
StringnewRandom(int size, int price, List oldRandomList)
new Random
if (size > 500) {
    return newRandomV2(size, price, oldRandomList);
Map<String, Integer> randomMap = new HashMap<String, Integer>();
int r = 10000000;
for (int i = 1; i <= price; i++) {
    randomMap.put(String.valueOf(r + i), new Integer(0));
for (int i = 0; i < oldRandomList.size(); i++) {
    randomMap.put(oldRandomList.get(i), new Integer(1));
List<String> ranList = new ArrayList<String>();
for (int j = 0; j < size; j++) {
    if (randomMap.size() > 0) {
        int range = randomMap.size() - oldRandomList.size() - j;
        if (range < 1) {
            range = 1;
        int ran = new Random().nextInt(range);
        int max = Math.min(ran + oldRandomList.size() + size, randomMap.size());
        String[] keys = new String[randomMap.size()];
        randomMap.keySet().toArray(keys);
        for (int k = 0, x = 0; k < max; k++) {
            String key = keys[k];
            Integer flag = randomMap.get(key);
            if (flag == 0) {
                if (x == ran) {
                    ranList.add(key);
                    randomMap.put(key, new Integer(1));
                    break;
                x++;
return ranList.toString().replaceAll("\\s+", "").replace("[", "").replace("]", "");
CollectionnewRandomIntegerList(int size)
Create a collection containing the given number of random integers.
if (size < 0) {
    throw new IllegalArgumentException("Size cannot be less than 0. ");
Collection<Integer> integers = new ArrayList<Integer>(size);
for (int i = 0; i < size; i++) {
    integers.add(RANDOM.nextInt());
return integers;
...