Getting Random Elements from ArrayList in Java

07 Jan, 2021
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java.
Generating random numbers from the list have a good utility value 
There are various methods to get a random element from the ArrayList:
Using Math.random()Using ArrayList ShuffleUsing Random class
Using Math.random()
Using ArrayList Shuffle
Using Random class
Method 1: Using Math.random()
Syntax :
public static double random()

Return :This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
Math.random() generates a random value between 0 and 1. 
Multiply this value with the size of ArrayList and take only the integer part of this value.
Now the number that is generated after performing the above steps can be taken as the index of the ArrayList.
Use the get() method to return a random element from the ArrayList using the above-generated index.
Note: Each time the Math.random() method is called it generates a new random value however original order of elements in the ArrayList does not get disturbed.
Java
// Java program for Getting Random Elements // from ArrayList using Math.random() function  import java.io.*;import java.util.ArrayList;class GFG {    public static void main(String[] args)    {        // creating ArrayList        ArrayList<Integer> my_list = new ArrayList<Integer>();          // adding elements        my_list.add(10);        my_list.add(80);        my_list.add(30);        my_list.add(70);        my_list.add(5);        my_list.add(90);        my_list.add(19);        my_list.add(25);          // loop to print elements at randonm        for (int i = 0; i < my_list.size(); i++)         {           // generating the index using Math.random()            int index = (int)(Math.random() * my_list.size());              System.out.println("Random Element is :"                               + my_list.get(index));        }    }}
Random Element is :70
Random Element is :25
Random Element is :90
Random Element is :5
Random Element is :70
Random Element is :30
Random Element is :70
Random Element is :10
Method 2: Using ArrayList Shuffle
Collections.shuffle() method of Java shuffles the order of elements in ArrayList.
Now we can simply iterate over the ArrayList and return elements as they are in random order now.
 Note: The actual ArrayList itself gets shuffled and original ordering is lost.
Java
// Java program for Getting Random Elements // from ArrayList using Collections.shuffle()  import java.io.*;import java.util.*;import java.util.ArrayList;class GFG {    public static void main(String[] args)    {        // creating ArrayList        ArrayList<Integer> my_list = new ArrayList<Integer>();          // adding elements        my_list.add(10);        my_list.add(80);        my_list.add(30);        my_list.add(70);        my_list.add(5);        my_list.add(90);        my_list.add(19);        my_list.add(25);          // using collections.shuffle to shuffle elements of        // ArrayList        Collections.shuffle(my_list);          // using for each loop to print values at random        System.out.println("Random values :");                for (Integer random_values : my_list)        {            System.out.print(random_values + " ");        }    }}
Random values :
19 5 25 90 10 30 80 70
Method 3: Using Random Class function
nextInt() method of Random class can be used to generate a random value between 0 and the size of ArrayList.
Now use this number as an index of the ArrayList.
Use get () method to return a random element from the ArrayList using number generated from nextInt() method.
Note: The original order of the ArrayList remains the same.
Java
// Java program for Getting Random Elements // from ArrayList using nextInt() function  import java.io.*;import java.util.ArrayList;import java.util.Random;class GFG {    public static void main(String[] args)    {        // creating ArrayList        ArrayList<Integer> my_list = new ArrayList<Integer>();          // adding elements        my_list.add(10);        my_list.add(80);        my_list.add(30);        my_list.add(70);        my_list.add(5);        my_list.add(90);        my_list.add(19);        my_list.add(25);          // initializing random class        Random random_method = new Random();          // loop for generation random number        for (int i = 0; i < my_list.size(); i++)         {            // generating random index with the help of            // nextInt() method            int index = random_method.nextInt(my_list.size());              System.out.println("Random Element is :"                               + my_list.get(index));        }    }}
Random Element is :5
Random Element is :10
Random Element is :19
Random Element is :5
Random Element is :30
Random Element is :70
Random Element is :25
Random Element is :90
Java-ArrayList
Java-Collections
Picked
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.