🌐
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 - Here, you need to make sure that element is removed from the list after selection: public void givenList_whenNumberElementsChosen_shouldReturnRandomElementsNoRepeat() { Random rand = new Random(); List<String> givenList = Lists.newArrayList("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); givenList.remove(randomIndex); } }
🌐
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
Answer (1 of 4): It is quite easy. 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.
🌐
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 - 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");
🌐
Dirask
dirask.com › posts › Java-pick-random-string-from-array-of-strings-y1xln1
Java - pick random string from array of strings
We need to generate random index of an array. As nextInt function is called we pass int bound to have results within array size. import java.util.concurrent.ThreadLocalRandom; public class JavaRandomStringFromArray { public static String randomStringFromArr() { String[] arr = {"A", "B", "C", "D", "E", "F"}; int randIdx = ThreadLocalRandom.current().nextInt(arr.length); String randomElem = arr[randIdx]; return randomElem; } public static void main(String[] args) { System.out.println(randomStringFromArr()); // F System.out.println(randomStringFromArr()); // B System.out.println(randomStringFromArr()); // A } }
🌐
openHAB Community
community.openhab.org › setup, configuration and use › scripts & rules
HELP - How to get a random string item from list - Scripts & Rules - openHAB Community
February 22, 2016 - Hello, I’m trying to get a random string from a list of quotes defined inside of an OpenHAB rule, and having difficulty understanding how to do it. I’ve tried a variety of seemingly standard java ways to do this, but t…
🌐
CodeJava
codejava.net › coding › generate-random-strings-examples
Generate Random Strings in Java Examples
Random String #1: !`;u!k=|Vc Random String #2: %%xB\B25ryhg|zS Random String #3: K/IzJ'}e@z$Vo%`'.Il)You can use this code example to generate random strong passwords. You can use the java.util.UUID class to generate random strings that are kind of Universally Unique Identifier (UUID).
🌐
YouTube
youtube.com › team mast
Return a Random item from a list in Java | Team MAST - YouTube
You will learn 3 methods on how to generate a random number and based on that you will see how to return a random item from a list in Java. In practice, the ...
Published   June 4, 2016
Views   12K
Find elsewhere
🌐
Xperti
xperti.io › home › generate random string in java
Easiest Ways To Generate A Random String In Java
May 9, 2022 - You can also generate a random alphanumeric string of fixed length using streams in Java. A random string is generated by first generating a stream of random numbers of ASCII values for 0-9, a-z and A-Z characters.
Top answer
1 of 6
1
Random r = new Random();

creates a pseudo-random list of values. To get other results with each run of the program use:

Random r = new Random(System.currentTimeMillis());

instead.

2 of 6
1

You're asking to select a random value from a list, and you mention wanting to "reset" after each selection. This is commonly known as "sampling with replacement" meaning that when you select a sample you don't change the probability that you'll select it again the next time. Imagine a drawing cards from a deck - with replacement you put the card back and reshuffle, without replacement you set it aside and draw from the (now smaller) deck.

With-replacement sampling is easy to implement in Java, simply use Random.nextInt(int) to select a random value between 0 and the size of your list. The returned integer is the index to get from your list.

Without-replacement sampling is a little tricker (or requires a different data structure) since you have to either modify the original list or store the set of previously-seen values. which you should prefer depends on how many values you'll need to select.

Several people have suggested Collections.shuffle() which also works, but is more invasive than simply generating a random index. Random.nextInt() is O(1) and doesn't modify your list in any way. Collections.shuffle() has to iterate over the entire list, taking O(n log n) time, and modifies your list. It's useful if you need to select many values from the list, or want without-replacement selection (in which case you shuffle once, then simply iterate over the list).

Top answer
1 of 3
5
public static void main(String[] args) throws InterruptedException {
    List<String> my_words = new LinkedList<String>();
    my_words.add("1153 3494 9509 2 0 0 0 0");
    my_words.add("1153 3487 9509 2 0 0 0 0");
    my_words.add("1153 3491 9525 2 0 0 0 0");
    my_words.add("1153 3464 9513 2 0 0 0 0");

    Random rand = new Random();
    while (true) {
        int choice = rand.nextInt(my_words.size());
        System.out.println("Choice = " + my_words.get(choice));
        Thread.sleep(1000);
        int replaceTo = rand.nextInt(my_words.size());          
        System.out.println("Replace to = " + my_words.get(replaceTo));
        my_words.set(choice, my_words.get(replaceTo));          
    }
}
2 of 3
1

If you have a list/array of data and you want to select a random element from the list. The easiest would probably be to generate a random number with the Math.random (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html) function which is between 0 and the count of your list/array.

You can then create a Thread that runs forever and sleeps for 7200 seconds between executions that generates a new random number and replaces the old variable.

Just watch out for concurrency issues when using Multi-Threading, have a read at http://download.oracle.com/javase/tutorial/essential/concurrency/.

Update (Example):

Java has a list that can be used to add, and remove data as you want. Data can then by extracted by giving the list the index (number) where the data is located in the list.

So you would be creating a list, then generating a random number in the list's range (0 to the size of the list as the max). And then extracting the data from the list by giving the list your random index. A example would be:

List<String> my_words = new LinkedList<String>();
my_words.add("1153 3494 9509 2 0 0 0 0");
my_words.add("1153 3487 9509 2 0 0 0 0");
my_words.add("1153 3491 9525 2 0 0 0 0");
my_words.add("1153 3464 9513 2 0 0 0 0");

//Maybe a loop to load all your strings here...

Random random = new Random(); //Create random class object
int randomNumber = random.nextInt(my_words.size()); //Generate a random number (index) with the size of the list being the maximum
System.out.println(my_words.get(randomNumber)); //Print out the random word

Hope this makes a bit more sense, and on second thought the Random class in java.util . Would be easier to rap your head around.

🌐
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())); } }
🌐
Programiz
programiz.com › java-programming › examples › generate-random-string
Java Program to Create random strings
import java.util.Random; class ... = alphabet.charAt(index); // append the character to string builder sb.append(randomChar); } String randomString = sb.toString(); System.out.println("Random String is: " + randomString); } }...
🌐
SpigotMC
spigotmc.org › threads › pick-random-string-from-list.445761
Solved - Pick random string from list | SpigotMC - High Performance Minecraft Software
I'm trying to get a random string from a list that's in the config and for example, print the random string when someone does /randomstring but I have...
🌐
GeeksforGeeks
geeksforgeeks.org › java › generate-random-string-of-given-size-in-java
Generate random String of given size in Java - GeeksforGeeks
July 11, 2025 - // Java program generate a random AlphaNumeric String // using Math.random() method public class RandomString { // function to generate a random string of length n static String getAlphaNumericString(int n) { // choose a Character random from ...
🌐
Baeldung
baeldung.com › home › java › java string › java – generate random string
Java - Generate Random String | Baeldung
May 11, 2024 - Through different implementation methods, we were able to generate bound and unbound strings using plain Java, a Java 8 variant, or the Apache Commons Library. In these Java examples, we used java.util.Random, but one point worth mentioning is that it is not cryptographically secure.
🌐
Coderanch
coderanch.com › t › 562577 › java › SOLVED-randomly-String-array-random
[SOLVED]How to randomly get a String from an array in a random amount of times. [Solved] (Beginning Java forum at Coderanch)
How to randomly get a String from an array, but the BIG money question I am asking for today is how to get a random string from an array by a Random amount of cycles , and printing it on the screen. My idiotic brain did not realize to just add the below code in the for loop.