Randomly select items from a List in Java


List is the sub-interface of Java Collection Interface. It is a linear structure that stores and accesses each element in a sequential manner. To use the features of list, we use ArrayList and LinekdList class that implements the list interface. In this article, we will create an ArrayList and try to select items from that list randomly.

Program to Randomly select items from a List in Java

Random Class

We create an object of this class to generate pseudorandom numbers. We will customize this object and apply our own logic to select any random items from the list.

Syntax

Random nameOfObject = new Random();

Example 1

The following example illustrates how to use the object of ‘Random’ class to select a single item from specified list.

Approach

  • Create an ArrayList and store some elements in it by using ‘add()’ method.

  • Define an object of class ‘Random’.

  • The object will check whole list and select an item using ‘nextInt()’ method. Then, using ‘get()’ method we will fetch that item and store it in an integer variable.

Example

import java.util.*;
public class Randomly {
   public static void main(String[] args) {
     // Creating arraylist 
     ArrayList<Integer> araylist = new ArrayList<Integer>();
     // Adding elements in arraylist
     araylist.add(8);
     araylist.add(5);
     araylist.add(2);
     araylist.add(9);
     araylist.add(4);
     araylist.add(7);
     System.out.println("Elements of the list : ");
     // loop to iterate through elements
     for(int i = 0; i < araylist.size(); i++ ) {
       // to print the elements in the list
       System.out.println(araylist.get(i)); 
     }
     Random rndm = new Random(); 
     // creating object
     int rndmElem = araylist.get(rndm.nextInt(araylist.size()));
     System.out.println("Selecting a random element from the list : " + rndmElem);
   }
}

Output

Elements of the list : 
8
5
2
9
4
7
Selecting a random element from the list : 8

Example 2

The object of class ‘Random’ may select one element twice from the list. This example demonstrates how we can select multiple items from the list.

Approach

  • Create an ArrayList and store some elements in it by using ‘add()’ method.

  • Define an object of class ‘Random’.

  • Now, declare an integer variable named ‘noOfrndmElem’ that will store the number of items to be selected.

  • Create a for loop that will run till the ‘noOfrndmElem’ and select the items.

import java.util.*;
public class Randomly {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.println(araylist.get(i)); 
      }
      Random rndm = new Random();
      int noOfrndmElem = 2;
      System.out.println("Selecting two elements randomly from the list : ");
      for (int i = 0; i < noOfrndmElem; i++) {
         int rndmIndx = rndm.nextInt(araylist.size());
         Integer rndmElem = araylist.get(rndmIndx);
         System.out.print(rndmElem + " ");
      }
   }
}

Output

Elements of the list : 
8
5
2
9
4
7
Selecting two elements randomly from the list : 
8 2 

Example 3

Earlier we discussed that the object of class ‘Random’ may select one element twice from the list. This example demonstrates how we can avoid this situation.

Approach

In the same code, we create a for loop that will run till the ‘noOfrndmElem’ and select the items. After selecting, it will remove that element from list using the built-in method ‘remove()’. We are accessing and deleting the element through index.

import java.util.*;
public class Randomly {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.println(araylist.get(i)); 
      }
      Random rndm = new Random();
      int noOfrndmElem = 2;
      System.out.println("Selecting two elements randomly from the list : ");
      for (int i = 0; i < noOfrndmElem; i++) {
         int rndmIndx = rndm.nextInt(araylist.size());
         Integer rndmElem = araylist.get(rndmIndx);
         System.out.print(rndmElem + " ");
         araylist.remove(rndmIndx);
      }
   }
}

Output

Elements of the list : 
8
5
2
9
4
7
Selecting two elements randomly from the list : 
7 2 

Conclusion

In this article, we have discussed a few Java programs to randomly select items from the list. We started with defining the list and then a class named ‘Random’ that is used to generate random numbers. We have defined a custom logic and applied it with the object of class ‘Random’ to select items randomly.

Updated on: 16-May-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements