Open In App

Randomly Select Items from a List in Java

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to efficiently select an element from a list in Java. The basic approach involves generating a random index between 0 and the size of the list, and then using that index to retrieve the item.

Different Ways to Select Items from a List

Below are the different approaches to selecting random elements from a list.

1. Select a Single Random item

The easiest way to select a random item is by generating a random index using the Java Random class.

Example: This example demonstrates how to select a single random element.

Java
// Randomly select an element from a list
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Geeks {
    public static void main(String[] args)
    {

        // creating a list of integer type
        List<Integer> l = new ArrayList<>();
        l.add(10);
        l.add(20);
        l.add(30);
        l.add(40);
        l.add(50);

        Geeks o = new Geeks();

        // take a random element from list and print them
        System.out.println("Random Element: "
                           + o.getRandomElement(l));
    }

    // Function select an element base on index
    // and return an element
    public int getRandomElement(List<Integer> l)
    {
        Random r = new Random();
        return l.get(r.nextInt(l.size()));
    }
}

Output
Random Element: 50

2. Selecting a Random Element in a Multi-threaded Environment

In multi-threaded applications, if multiple threads use the same Random object, they might interfere with each other and cause unexpected results. To fix, this Java provides ThreadLocalRandom class, which gives each thread its own random number generator and prevent any interference.

Example: This example demonstrates how to randomly select an element from a list using ThreadLocalRandom.

Java
// Java Program to demonstrate the working 
// of ThreadLocalRandom class
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Geeks {
    public static void main(String[] args) {
        List<Integer> l = new ArrayList<>();
        l.add(10);
        l.add(20);
        l.add(30);
        l.add(40);
        l.add(50);

        Geeks o = new Geeks();
        System.out.println("Random Element: " + o.getRandomElement(l));
    }

    public int getRandomElement(List<Integer> l) {
        return l.get(ThreadLocalRandom.current().nextInt(l.size()));
    }
}

Output
Random Element: 50

3. Selecting Multiple Random Items With Repetitions

Sometimes, we want to select multiple random items from a list, allowing repetitions. In this case, we can keep selecting random elements without removing them from the list.

Example: This example demonstrates how to randomly select multiple elements from a list.

Java
// Select randomly multiple elements
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Geeks {
    public static void main(String[] args) {
        List<Integer> l = new ArrayList<>();
        l.add(10);
        l.add(20);
        l.add(30);
        l.add(40);
        l.add(50);

        Geeks o = new Geeks();
        System.out.println("Random Elements: " 
        + o.getRandomElements(l, 3));
    }

    public List<Integer> getRandomElements(List<Integer> l, int totalItems) {
        Random r = new Random();
        List<Integer> newList = new ArrayList<>();

        for (int i = 0; i < totalItems; i++) {
            int randomIndex = r.nextInt(l.size());
            newList.add(l.get(randomIndex));
        }

        return newList;
    }
}

Output
Random Elements: [50, 10, 50]

4. Selecting Multiple Random Items Without Repetitions

In some cases, we may want to select multiple random items without repetitions. To do this, we can remove elements from the list after selecting them or use a Set to track previously selected items.

Example: This example demonstrates how to select random element from a list without repetition by removing the selected items from the original list.

Java
// Select random elements from a 
// list without repetition
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Geeks {
    public static void main(String[] args) {
        List<Integer> l = new ArrayList<>();
        l.add(10);
        l.add(20);
        l.add(30);
        l.add(40);
        l.add(50);

        Geeks o = new Geeks();
        System.out.println("Random Elements: " 
        + o.getRandomElements(l, 3));
    }

    public List<Integer> getRandomElements(List<Integer> l, int totalItems) {
        Random r = new Random();
        List<Integer> newList = new ArrayList<>();

        for (int i = 0; i < totalItems; i++) {
            int randomIndex = r.nextInt(l.size());
            newList.add(l.get(randomIndex));
            l.remove(randomIndex);  // Remove selected item
        }

        return newList;
    }
}

Output
Random Elements: [30, 10, 50]


Next Article

Similar Reads