Open In App

How to Get Random Elements From the Vector in Java?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Vector in java is a part of Java’s collections framework. Vector is a dynamic array of objects, i.e., the size of the vector can be modified as per the requirement. Vector implements the List interface. It also maintains the insertion order and the elements of the vector can be accessed using their indexes. The vector in java is synchronized

We can access the vector elements in the following ways:

  • Using their indexes
  • By using the iterator
  • By randomly calling the elements from the vector

Different ways to get random elements from the vector:

  1. Using random() method of Math class
  2. Using Random class
  3. Using ThreadLocalRandom class

Method 1: Using random() method of Math class

The Math class of the java.lang package has a method random() which returns a positive double value which is greater than 0.0 and less than 1.0. We can use this method to generate a random index and access the element present at that index in the given vector.

Java




// Java program to access random elements from the vector
// using random() method of Math class
  
import java.util.Vector;
public class GfG {
    
    // Create an instance of type vector which accepts
    // elements of String type
    static Vector<String> vector;
  
    // getRandomElements() method which accesses random
    // elements from the vector
    static void getRandomElements()
    {
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++) 
        {
            // Generate a random int value between 0 and the
            // last index of the vector
            int index = (int)(Math.random() * vector.size());
            
            // get the element at the generated random index
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Instantiate the vector instance
        vector = new Vector<String>();
        
        // Add elements into the vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getElements() method on this vector
        // object
        getRandomElements();
    }
}


Output

For
To
Welcome
Welcome
Geeks

Method 2: Using Random class

To generate the random index we can also use the Random class of the java.util package. It provides useful methods to generate random numbers of the specified type and within specified ranges.

Java




// Java program to access random elements from the vector
// using random class
  
import java.util.Random;
import java.util.Vector;
class GFG {
    
    // Create a Vector instance of type String
    static Vector<String> vector;
    
    // getRandomElements() method which accesses the
    // random elements from the given vector
    static void getRandomElements()
    {
        // create new object of the Random class
        Random random = new Random();
        
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++)
        {
            // call the nextInt() method of this random
            // object to generate a random int value
            int index = random.nextInt(vector.size());
            
            // Get the element present at this random index
            // in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Instantiate the vector object
        vector = new Vector<String>();
        
        // Add elements into the Vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}


Output

Welcome
Geeks
To
Geeks
For

Method 3: Using ThreadLocalRandom class

ThreadLocalRandom class of the java.util.concurrent is a random number generator isolated to the current thread. It generates a random number of a specified type and within the specified range for each thread in a multi-threading environment. We can generate the random index using the nextInt() method of this class and access the element at that index from the given vector. 

Java




// Java program to access random elements from the given
// vector using ThreadLocalRandom class
  
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class GFG {
    
    // Create a Vector instance which accepts elements of
    // String type
    static Vector<String> vector;
    
    // getRandomElements() method which accesses random
    // elements from the given vector
    static void getRandomElements()
    {
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++) 
        {
            // Generate a random integer by calling the
            // ThreadLocalRandom.current().nextInt() method
            int index = ThreadLocalRandom.current().nextInt(vector.size());
            
            // Print the element present at this random
            // index in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        vector = new Vector<String>();
        
        // Add elements into the vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}


Output

Geeks
Welcome
Geeks
To
To


Last Updated : 07 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads