public static int getRandom(int[] array) {
    int rnd = new Random().nextInt(array.length);
    return array[rnd];
}
Answer from Chris Dennett on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ java โ€“ get random item/element from a list
Java - Get Random Item/Element From a List | Baeldung
April 4, 2025 - Sometimes you might want to pick few elements from a list. It is quite straightforward: public void givenList_whenNumberElementsChosen_shouldReturnRandomElementsRepeat() { Random rand = new Random(); List<String> givenList = Arrays.asList("one", "two", "three", "four"); int numberOfElements = 2; for (int i = 0; i < numberOfElements; i++) { int randomIndex = rand.nextInt(givenList.size()); String randomElement = givenList.get(randomIndex); } } Here, you need to make sure that element is removed from the list after selection: public void givenList_whenNumberElementsChosen_shouldReturnRandomEleme
Discussions

Is there a smart way to get UNIQUE random elements from a collection in Java?
This turns out to be one of those things that smart people spent a bit too long discussing. That's because you're effectively asking "how do I efficient create a 1:1 mapping from one number (a counter in your case) to another (a random permutation of those same numbers)," and that's a relevant question for encryption and a few other things. That means you're basically constructing a form of "Format-preserving encryption:" https://en.wikipedia.org/wiki/Format-preserving_encryption But that's a bit crazy, so might I suggest that you do one of two things: If the upper bound is fairly small (say, less than 10,000), make a list of all of the elements and then shuffle it. Now just use the numbers from the beginning to the end of the list in order. If the upper bound is quite large but you only need a few numbers (say less than 10% of the range), store a set of numbers you've already used and just generate another random number if you encounter a repetition. If the upper bound is quite large and you need a whole lot of random numbers, it's probably time to start thinking about block cipher algorithms or linear congruential generators. More on reddit.com
๐ŸŒ r/learnprogramming
9
1
January 30, 2023
java - Random element from string array - Stack Overflow
-2 How do you make the computer select random elements from an array? More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 30, 2017
Picking a random item from an array of strings in java - Stack Overflow
I have some arrays containing Strings and I would like to select randomly an item from each array. How can I accomplish this? Here are my arrays: static final String[] conjunction = {"and", "or",... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 20, 2015
Get a random object from a array which is in a JSON file

You can select a random object from an array like this,

const quotes = [{},..];

const quote = quotes[Math.floor(Math.random() * quotes.length)];

Now, quote will contain a random object from the quotes array.

More on reddit.com
๐ŸŒ r/reactjs
5
3
December 14, 2016
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ randomly-select-items-from-a-list-in-java
Randomly Select Items from a List in Java - GeeksforGeeks
July 11, 2025 - Example: This example demonstrates how to randomly select an element from a list using ThreadLocalRandom. ... // Java Program to demonstrate the working // of ThreadLocalRandom class import java.util.ArrayList; import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class Geeks { public static void main(String[] args) { List<Integer> l = new ArrayList<>(); l.add(10); l.add(20); l.add(30); l.add(40); l.add(50); Geeks o = new Geeks(); System.out.println("Random Element: " + o.getRandomElement(l)); } public int getRandomElement(List<Integer> l) { return l.get(ThreadLocalRandom.current().nextInt(l.size())); } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ java-program-to-generate-a-random-number-from-an-array
Java program to generate a random number from an array
November 4, 2024 - import java.util.Random; public class Demo { public static void main(String... args) { int[] arr = new int[] { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; System.out.print("Random number from the array = "+arr[new Random().nextInt(arr.length)]); } ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 676314 โ€บ java โ€บ Choose-random-element-array
Choose random element from array (Beginning Java forum at Coderanch)
February 17, 2017 - Just like there's nothing much gained from shuffling a deck of cards more than seven times. ... For a List of any size, the number of possible combinations will rapidly exhaust the randomisation of shuffle; for lottery numbers there are 49! permutations. Maybe the random removal technique would therefore be better. Or would you say that at the beginner's stage nobody will notice? I know some people will say this is an advanced technique, but you can create a sequential array or List with an IntStream.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ getting-random-elements-from-arraylist-in-java
Getting Random Elements from ArrayList in Java - GeeksforGeeks
July 23, 2025 - 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 ...
๐ŸŒ
YouTube
youtube.com โ€บ asim code
How to randomly pick an element from an array in Java - YouTube
In this video we will learn how to randomly pick an element from an array in Java. Please subscribe to support Asim Code!https://www.youtube.com/channel/UC2w...
Published ย  August 5, 2014
Views ย  8K
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-I-pick-up-a-random-string-from-an-array-of-strings-in-Java
How to pick up a random string from an array of strings in Java - Quora
You only need to generate a random number that acts as the index value for String array. You can generate random value using Random class defined in java.util package. ... Keep teams on track.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ is there a smart way to get unique random elements from a collection in java?
r/learnprogramming on Reddit: Is there a smart way to get UNIQUE random elements from a collection in Java?
January 30, 2023 -

Right now I'm using the Random class, which .nextInt(upperbound) provides me with random integers that can be used to index an ArrayList object. I want to get several random elements and want them to be unique, however.

I can come up with some solutions, but none of them are very elegant. Any ideas?

Edit: current solution is to remove the randomly selected element, but I also have to decrease the upperbound (myList.size() - i ) on each iteration of the for-loop.
I know that Pyhton has a function that picks n unique random elements from a list, is there anything similar in Java?

Top answer
1 of 6
3
This turns out to be one of those things that smart people spent a bit too long discussing. That's because you're effectively asking "how do I efficient create a 1:1 mapping from one number (a counter in your case) to another (a random permutation of those same numbers)," and that's a relevant question for encryption and a few other things. That means you're basically constructing a form of "Format-preserving encryption:" https://en.wikipedia.org/wiki/Format-preserving_encryption But that's a bit crazy, so might I suggest that you do one of two things: If the upper bound is fairly small (say, less than 10,000), make a list of all of the elements and then shuffle it. Now just use the numbers from the beginning to the end of the list in order. If the upper bound is quite large but you only need a few numbers (say less than 10% of the range), store a set of numbers you've already used and just generate another random number if you encounter a repetition. If the upper bound is quite large and you need a whole lot of random numbers, it's probably time to start thinking about block cipher algorithms or linear congruential generators.
2 of 6
3
An optimal choice is probably to 'shuffle' an int[], initially filled with a continuous sequence of values over the range you want, and then 'taking' from the array using an 'index'. Taking from the array in reverse will allow the 'index' to serve as a count of remaining values, a trivial stop-point to test against zero, and the index itself. Alternatively, you can fill a List (an ArrayList is the best option), with a continuous sequence of values over the range you want, and then use Collections.shuffle(list). Taking values from the end of the list will be the most optimal choice - eg Integer value = list.remove(list.size() - 1). Note: the method of randomisation for Collections.shuffle is documented in the Javadoc and would be trivial to apply to an int[].
๐ŸŒ
ExamTray
examtray.com โ€บ java โ€บ java-program-how-print-random-element-or-index-array-arraylist
Java Program: How to Print or Get or Find a Random Element of An Array, ArrayList | ExamTray
September 25, 2019 - This gives us a random Array Index at any point of time and every time. If we print the Array Element with this random index, what we get is a random element of the said Array. It is more or like picking a random string from a group of strings.
๐ŸŒ
Crunchify
crunchify.com โ€บ java j2ee tutorials โ€บ in java how to get random element from arraylist and threadlocalrandom usage
In Java How to Get Random Element from ArrayList and ThreadLocalRandom Usage โ€ข Crunchify
July 23, 2025 - When // applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically // encounter much less overhead and contention. private static void getRandomInteger() { int crunchifyInteger = ThreadLocalRandom.current().nextInt(1, 50); log("RandomInteger: " + crunchifyInteger); } private static void getRandomDouble() { double crunchifyDouble = ThreadLocalRandom.current().nextDouble(1, 250); log("RandomDouble: " + crunchifyDouble); } public static String getRandomCompany() { ArrayList<String> companyName = new ArrayList<String>(); companyName.add("Google");
๐ŸŒ
YouTube
youtube.com โ€บ watch
6.6 Array in Java Tutorial With Example using Random Class - YouTube
In this video we will assign random values in an array. Java provides a class called Random which belongs to package called java.util. nextInt() is a method ...
Published ย  July 7, 2022
๐ŸŒ
CodingNConcepts
codingnconcepts.com โ€บ java โ€บ generate-random-numbers-in-java
How to generate Random Numbers in Java - Coding N Concepts
July 11, 2025 - You can get a random element from an Array of elements by generating a random index within the length range of the Array.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 239268 โ€บ randomly-select-elements-from-array
java - Randomly select elements from array [SOLVED] | DaniWeb
May 16, 2023 - I'm not so worried about multiple elements at the moment. ... Note that get() is used if 'unsorted' is an arraylist and not an array. ... Note that get() is used if 'unsorted' is an arraylist and not an array. Thank you. I changed to .get, says I have incompatible types. Says found .Object, requires Int ยท Could you point out where I went wrong in this? Appreciate it. import java.util.*; public class Lab5 { public static void main(String[] args) { Random random = new Random(); ArrayList unsorted = new ArrayList(), sorted = new ArrayList(); int[] keys_Array = new int[100]; for(int count = 0; co
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ randomly-select-array-element-java
How to Randomly Select an Element from an Integer Array in Java? - CodingTechRoom
October 2, 2020 - import java.util.Random; public class RandomElementPicker { public static void main(String[] args) { int[] array = {1, 2, 3}; Random random = new Random(); int randomIndex = random.nextInt(array.length); int randomElement = array[randomIndex]; System.out.println("Randomly selected element: ...
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ java โ€บ picking-a-random-element-from-a-set-in-java.html
Picking a random element from a set in java
5 days ago - import java.util.*; public class ... int randomIndex = random.nextInt(stringArray.length); // Get the random element from the array String randomElement = stringArray[randomIndex]; // Print the random element System.out.println("Random element from the set: " + randomElement); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ generate-a-random-array-of-integers-in-java
Generate a random array of integers in Java
January 29, 2023 - import java.util.Random; public class Example { public static void main(String[] args) { Random rd = new Random(); // creating Random object int[] arr = new int[5]; for (int i = 0; i < arr.length; i++) { arr[i] = rd.nextInt(); // storing random integers in an array System.out.println(arr[i]); // printing each array element } } } -1848681552 39846826 858647196 1805220077 -360491047 ยท
๐ŸŒ
YouTube
youtube.com โ€บ kirupa
Picking a Random Item from an Array - YouTube
Using just a single line of code, learn how to randomly pick an item from an array.๐Ÿ“„ Read article: https://www.kirupa.com/html5/picking_random_item_from_arr...
Published ย  February 19, 2023
Views ย  9K