๐ŸŒ
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); } }
Discussions

HELP - How to get a random string item from list
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 to no avail; must be missing something. More on community.openhab.org
๐ŸŒ community.openhab.org
0
0
February 22, 2016
java - Getting a random String from a List - Stack Overflow
I am trying to create a list of words and I want to randomly pick one to use in a String. Here is what I currently have: private List words = new ArrayList (); public void More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I pick a random string from my list.
boysName[indexNum] for example boysName[0]. Then use a random number generator with Math.random. google how it works for examples and do boysName[rng] More on reddit.com
๐ŸŒ r/javahelp
7
9
August 23, 2018
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
๐ŸŒ
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
May 17, 2024 - 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โ€ฆ
๐ŸŒ
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())); } }
๐ŸŒ
CodeJava
codejava.net โ€บ coding โ€บ generate-random-strings-examples
Generate Random Strings in Java Examples
January 13, 2023 - 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).
Find elsewhere
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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); } }...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
SpigotMC
spigotmc.org โ€บ threads โ€บ pick-random-string-from-list.445761
Solved - Pick random string from list | SpigotMC - High Performance Minecraft Software
June 25, 2018 - 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...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java : return a random item from a list
Java : Return a random item from a List - Mkyong.com
August 5, 2014 - Random example to get a random item from an ArrayList. ... package com.mkyong; import java.util.ArrayList; import java.util.List; import java.util.Random; public class RandomExample { private Random random = new Random(); public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Apple"); list.add("Boy"); list.add("Cat"); list.add("Dog"); list.add("Elephant"); RandomExample obj = new RandomExample(); for(int i = 0; i < 10; i++){ System.out.println(obj.getRandomList(list)); } } public String getRandomList(List<String> list) { //0-4 int index = random.nextInt(list.size()); System.out.println("\nIndex :" + index ); return list.get(index); } }
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ create-random-strings
Java Program to Create random strings | Vultr Docs
November 25, 2024 - It builds a string of the specified length by randomly selecting characters from the predefined characterSet. The use of StringBuilder ensures that the string construction is efficient. Use Java 8's Stream API in combination with Random to create a one-liner method for random string generation.