Java ArrayList get random elements example shows how to get random elements from ArrayList in Java. The example also shows how to get a random value from the ArrayList using various approaches.
How to get random elements from ArrayList in Java?
There are several ways using which you can get a random element from ArrayList as given below.
1) Get random element from ArrayList using the Random class
You can use nextInt
method of Random class to generate a random number between 0 and size of the ArrayList and get element at the generated random index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Random; public class ArrayListRandomElementExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Red"); aListColors.add("Yellow"); /* * To get random item/element from ArrayList, use * Random class along with get method of ArrayList */ Random random = new Random(); //lets get 5 random elements from ArrayList for(int i=0; i < aListColors.size(); i++){ System.out.println( aListColors.get( random.nextInt(aListColors.size()) ) ); } } } |
Output
1 2 3 4 5 |
Red Green Blue Blue Green |
2) Get random element from ArrayList using Math.random
You can use random
method of Math class to generate a random number and use that as an index in the get
method of the ArrayList class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Red"); aListColors.add("Yellow"); //generate random index using Math.random method int randomIndex = (int) (Math.random() * aListColors.size()); System.out.println( "Random Color: " + aListColors.get( randomIndex ) ); |
Output
1 |
Random Color: Red |
3) Get random element from ArrayList using ThreadLocalRandom (JDK 7 and above)
If you are using multithreading, it is better to use ThreadLocalRandom instead of Random class to generate the random numbers per thread for performance reasons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Red"); aListColors.add("Yellow"); //generate random index using ThreadLocalRandom class int randomIndex = ThreadLocalRandom.current().nextInt( aListColors.size()); System.out.println( "Random Color: " + aListColors.get( randomIndex ) ); |
Output
1 |
Random Color: Yellow |
4) Get random element from ArrayList using ArrayList shuffle
Another approach to get random items from ArrayList is to shuffle ArrayList and get the elements from it as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Red"); aListColors.add("Yellow"); //shuffle the list and get elements from it Collections.shuffle( aListColors ); System.out.println("Random elements: "); for(String color : aListColors) System.out.println(color); |
Output
1 2 3 4 5 6 |
Random elements: Blue Red Green Red Yellow |
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.
I think there will be a possibly error on option 2 cause it will give you a random between 0 and size of the list and the last index of the list is size() -1
Hello Diego,
Thanks for your comment. The random method generates a random number that is greater than or equal to 0 and always less than 1 (i.e. 0 <= number < 1). Thus, when we convert the multiplication to an int value, as I have done in the example, it will end up being 1 less than the size specified. For example, if the list size is 5 and the random number is 0.9999, the multiplication will be 5 * 0.9999 = 4.9995. When we convert it to an int, we get 4. I hope it clears your doubts.
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.
Hallo bro!
i would like to use random function to let 8 players play against each other.
how to code this