🌐
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 - In order to get a random item from a List instance, you need to generate a random index number and then fetch an item by this generated index number using List.get() method. The key point here is to remember that you mustn’t use an index that ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › getting-random-elements-from-arraylist-in-java
Getting Random Elements from ArrayList in Java - GeeksforGeeks
July 23, 2025 - Math.random() generates a random value between 0 and 1. Multiply this value with the size of ArrayList and take only the integer part of this value. Now the number that is generated after performing the above steps can be taken as the index ...
🌐
Coderanch
coderanch.com › t › 695754 › java › Picking-random-element-arrayList
Picking a random element from an arrayList [Solved] (Beginning Java forum at Coderanch)
June 25, 2018 - Sorry if I sound repetitive, but I don't have much of a grasp on Java, yet. ... Hey Nik, This has certainly been a lengthy conversation. So your line numbers might be skewed again but I think you're getting an error on this line ... and its because you create the Random object but you don't do anything with it. You're actually saying object * int = int. try looking at this page and see which method might return an int like you want. As far as the ArrayList conversation goes, the single line you quoted can go in place of line 3 in your code and you can remove lines 4-9.
🌐
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");
Top answer
1 of 2
1

rand.nextInt(10); gets a random number with no respect to the content of your list.

You need to pass the size of the list there, not literally 10 using myArray.size()

Secondly, if you want your code to do anything other than choose a random element from an empty list, you'll need to call that other method

It's a list, not an array

2 of 2
0

You never actually created the arrayList, so when you call the array in your showoperations method, it ends up trying to do the operations on an empty array. In addition, I tweaked your calculation for z so that it will give you a number no matter the sizeof the arrayList.

    import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class Operator {

Random rand = new Random();
ArrayList<String> myArray = new ArrayList<String>();

public void createArrayList() {
     myArray.add("add");
 myArray.add("subtract");
 myArray.add("multiply");
 myArray.add("divide");
 myArray.add("remainder");
 myArray.add("greaterthan");
 myArray.add("lessthan");
 myArray.add("max");
 myArray.add("min");
 myArray.add("power");
 try {
        FileReader inFile = new FileReader("data/numbers2.txt");
            Scanner scanner = new Scanner(inFile);
            String line = scanner.nextLine();
            System.out.println(line);

            scanner.close();
        } 
        catch (Exception ex) {
            ex.printStackTrace();}
    }

public void showOperations() {
    createArrayList();
    int x = (int) Math.floor(Math.random()*10);
    int y = (int) Math.floor(Math.random()*10);
    int z = rand.nextInt(myArray.size());
    System.out.println(x+" "+ myArray.get( z )+" "+ y );
    }
}
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
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.
🌐
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 } }
🌐
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
November 2, 2017 - import java.util.Random; public class MyClass { public static void main(String[] args) { int ary[] = {10,11,12,13,14,15}; Random ran = new Random(); int randomNumber = ran.nextInt(ary.length); int randomArrayElement = ary[randomNumber]; } } Last Minute Java Tutorial is already available on ...
🌐
TutorialsPoint
tutorialspoint.com › randomly-select-items-from-a-list-in-java
Randomly select items from a List in Java
May 16, 2023 - Create an ArrayList and store some elements in it by using ?add()? method. Define an object of class ?Random?. Now, declare an integer variable named ?noOfrndmElem? that will store the number of items to be selected. Create a for loop that will run till the ?noOfrndmElem? and select the items. import java.util.*; public class Randomly { public static void main(String[] args) { // Creating arraylist ArrayList<Integer> araylist = new ArrayList<Integer>(); // Adding elements in arraylist araylist.add(8); araylist.add(5); araylist.add(2); araylist.add(9); araylist.add(4); araylist.add(7); System.o
🌐
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 - Math.random() example to get a random item from an ArrayList. MathRandomExample.java · package com.mkyong; import java.util.ArrayList; import java.util.List; public class MathRandomExample { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); MathRandomExample obj = new MathRandomExample(); for(int i = 0; i < 10; i++){ System.out.println(obj.getRandomList(list)); } } public int getRandomList(List<Integer> list) { //Math.random() = greater than or equal to 0.0 and less than 1 //0-4 int index = (int)(Math.random()*list.size()); System.out.println("\nIndex :" + index ); return list.get(index); } } Concurrent Random in Java SE 7 ·
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-random-element-in-arraylist
Retrieve random value from Array List | Level Up Lunch
August 19, 2015 - @Test public void random_value_arraylist_java8() { List<String> randomValue = new ArrayList<String>(); randomValue.add("one"); randomValue.add("two"); randomValue.add("three"); Random random = new Random(); IntStream.range(0, 5).forEach( a -> System.out.println(randomValue.get(random .nextInt(randomValue.size())))); }
🌐
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 · // 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())); } } Output ·
🌐
SpigotMC
spigotmc.org › threads › get-random-string-from-array-list.282439
Solved - Get random string from array list? | SpigotMC - High Performance Minecraft Software
January 27, 2022 - Hi, I would like to know how I can get a random string for example: ArrayList: - test1 - test2 - test3 - test4 Get any random.
🌐
StackHowTo
stackhowto.com › home › java › how to randomly select an item from a list in java
How to randomly select an item from a list in Java - StackHowTo
June 17, 2020 - The elements of a list can be accessed directly via the index and the length is known: import java.util.*; public class Main { public static void main(String[] args) { List<String> names = Arrays.asList("Alex", "Amanda", "Emily", "Bob", "Jean"); ...
🌐
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…