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 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
import java.io.*;
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> my_list = new ArrayList<Integer>();
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 );
for ( int i = 0 ; i < my_list.size(); i++)
{
int index = ( int )(Math.random() * my_list.size());
System.out.println( "Random Element is :"
+ my_list.get(index));
}
}
}
|
Output
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
import java.io.*;
import java.util.*;
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> my_list = new ArrayList<Integer>();
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 );
Collections.shuffle(my_list);
System.out.println( "Random values :" );
for (Integer random_values : my_list)
{
System.out.print(random_values + " " );
}
}
}
|
Output
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
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> my_list = new ArrayList<Integer>();
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 );
Random random_method = new Random();
for ( int i = 0 ; i < my_list.size(); i++)
{
int index = random_method.nextInt(my_list.size());
System.out.println( "Random Element is :"
+ my_list.get(index));
}
}
}
|
Output
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Jan, 2021
Like Article
Save Article