public static int getRandom(int[] array) {
    int rnd = new Random().nextInt(array.length);
    return array[rnd];
}
Answer from Chris Dennett on Stack Overflow
🌐
Coderanch
coderanch.com › t › 676314 › java › Choose-random-element-array
Choose random element from array (Beginning Java forum at Coderanch)
February 17, 2017 - Change line 6 to match lines 2‑3 of previous code block to get a List instead. Change the argument in line 5 and you can get your 7 lottery numbers ready‑made as an array:) ... Junilu Lacar wrote:. . . I believe the Collections.shuffle() algorithm actually does that under the covers for you. . . . Don't know about the algorithm, but Joshua Bloch suggests that shuffle() isn't random enough for 52 cards. Can't remember whether it is in Effective Java or Java Puzzlers, but it sounds more likely to be the latter.
Discussions

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
How do I Pick the next random item from my array in java?
I don't want to straight give you the answer cause you won't learn anything. Look up for loops and array.pop. pop removes a value from the array but keeps it as a variable. More on reddit.com
🌐 r/learnprogramming
17
7
October 2, 2020
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 - Retrieving a random item from ArrayList - Stack Overflow
I'm learning Java and I'm having a problem with ArrayList and Random. I have an object called catalogue which has an array list of objects created from another class called item. I need a method in More on stackoverflow.com
🌐 stackoverflow.com
1 week ago
🌐
GeeksforGeeks
geeksforgeeks.org › java › getting-random-elements-from-arraylist-in-java
Getting Random Elements from ArrayList in Java - GeeksforGeeks
July 23, 2025 - 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.
🌐
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); } }
🌐
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)]); } ...
🌐
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   July 12, 2025
Views   8K
🌐
CodingTechRoom
codingtechroom.com › question › randomly-select-array-element-java
How to Randomly Select an Element from an Integer Array in Java? - CodingTechRoom
December 18, 2024 - import java.util.Random; public ... { 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: " + randomElement); } }...
Find elsewhere
🌐
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
June 25, 2018 - It is programmers need to choose or select or get a random element or random index of an Array or ArrayList in Java. The random element may be a number or string. Let us explore Math.random() method with examples. The same code can be used to implement a Lottery Draw to pick a random contestant ...
🌐
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.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 239268 › randomly-select-elements-from-array
java - Randomly select elements from array [SOLVED] | DaniWeb
August 13, 2024 - Also, after selection, how can I add these 100 values into a new array. ... Nice thread. Two quick clarifications to help future readers: (1) use the list’s size, not a hard-coded bound, when generating indices; that prevents out-of-bounds if the list size changes. (2) Type your collections (e.g., List<Integer>) so you do not have to cast. @Ezzaral is right that picking by random index can repeat the same position; if you want each element at most once, sample without replacement.
🌐
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())); } }
🌐
Coderanch
coderanch.com › t › 400428 › java › selecting-random-elements-array
selecting random elements from an array (Beginning Java forum at Coderanch)
August 3, 2005 - I have the following array and be able to select a random element from it each time the method is called:. Hope someone can help me with this.
🌐
Iditect
iditect.com › faq › java › picking-a-random-element-from-a-set-in-java.html
Picking a random element from a set in java
July 12, 2025 - In Java, you can pick a random element from a set by converting the set to an array and then using a random index to select an element from the array.
🌐
Reddit
reddit.com › r/learnprogramming › how do i pick the next random item from my array in java?
r/learnprogramming on Reddit: How do I Pick the next random item from my array in java?
October 2, 2020 -

Hello! I Am trying to learn to make random item generators, This is a slow process,. I Reall could use some help, I have this code as follows

String[] arr={"this","them","thee","when","words","it","its","thine"};

Random r=new Random();

int randomNumber=r.nextInt(arr.length);

System.out.println(arr[randomNumber]);

How would I pick The next random Item From the Array to System.out.print it.

please help me lol, I Would just use google but I am Not sure how I would even really word the search terms.

🌐
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[].
🌐
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
January 29, 2023 - Java is pretty amazing. Sometimes during mock testing you may need to generate Random number like Integer or Double or Long or String from ArrayList. In this tutorial we will create Company ArrayList and then we will retrieve random element from the list.
🌐
Stack Overflow
stackoverflow.com › questions › 21922695 › how-to-randomly-choose-from-a-varying-array-of-elements
java - How to randomly choose from a varying array of elements - Stack Overflow
November 1, 2023 - The first is probably the simplest to implement (adding a numberTrue++; and numberTrue--; in the appropriate code spots) - just a note here that if it's not possible to keep this count, you can achieve the same thing by running an initial for loop over your array and counting the number of true elements. The second way would lend itself to utilizing one of the answers from the other post: public int generateRandom(int start, int end, ArrayList<Integer> excludeRows) { Random rand = new Random(); int range = end - start + 1; int random = rand.nextInt(range) + 1; while(excludeRows.contains(random)) { random = rand.nextInt(range) + 1; } return random; }
🌐
Coderanch
coderanch.com › t › 695754 › java › Picking-random-element-arrayList
Picking a random element from an arrayList [Solved] (Beginning Java forum at Coderanch)
May 16, 2023 - You wish to select something from the list but your codes seems to be adding something to the list. ... Nik Recort wrote:But here it doesn't seem to work. Why not? What happens? Indexing is the same in lists and arrays, you just call the get() method instead of using the array's indexer.