public static int getRandom(int[] array) {
    int rnd = new Random().nextInt(array.length);
    return array[rnd];
}
Answer from Chris Dennett on Stack Overflow
🌐
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 - Retrieve and print the random element. ... { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; System.out.print("Random number from the array = "+arr[new Random().nextInt(arr.length)]); } }...
🌐
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.
🌐
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   October 27, 2023
Views   8K
🌐
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 - It is programmers need to choose or select or get or find a random element or number or string and a random index of an Array or ArrayList in Java. Let us explore Math.random() method with examples. The same code can be used to implement a Lottery Draw to pick a random contestant from a list ...
🌐
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
Java (programming languag... ... It is quite easy. You only need to generate a random number that acts as the index value for String array.
🌐
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 - public void givenList_shouldRe...t(givenList.size())); } Instead of Random class, you can always use static method Math.random() and multiply it with list size (Math.random() generates Double random value between 0 (inclusive) ...
🌐
TutorialsPoint
tutorialspoint.com › generate-a-random-array-of-integers-in-java
Generate a random array of integers in Java
September 12, 2023 - import java.util.Random; public ... ? The output might vary on Online Compilers. Here we use the nextInt() method in a loop to get a random integer for each element....
Find elsewhere
🌐
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");
🌐
CodingNConcepts
codingnconcepts.com › java › generate-random-numbers-in-java
How to generate Random Numbers in Java - Coding N Concepts
May 21, 2023 - In this tutorial, we’ll learn how to generate a random number, generate a random array or list, get a random element from an array or list, and shuffle the list elements. Java Random class is having many useful built-in methods for generating random numbers as follows:- nextInt(): Returns a random int value within the range: -2,147,483,648<= value <= 2,147,483, 647
🌐
Coderanch
coderanch.com › t › 676314 › java › Choose-random-element-array
Choose random element from array (Beginning Java forum at Coderanch)
February 17, 2017 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › randomly-select-items-from-a-list-in-java
Randomly Select Items from a List in Java - GeeksforGeeks
June 25, 2018 - // Randomly select an element from a list import java.util.ArrayList; import java.util.List; import java.util.Random; public class Geeks { public static void main(String[] args) { // creating a list of integer type List<Integer> l = new ArrayList<>(); l.add(10); l.add(20); l.add(30); l.add(40); l.add(50); Geeks o = new Geeks(); // take a random element from list and print them System.out.println("Random Element: " + o.getRandomElement(l)); } // Function select an element base on index // and return an element public int getRandomElement(List<Integer> l) { Random r = new Random(); return l.get(r.nextInt(l.size())); } } Output ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › add-random-number-to-an-array-in-java
How to Add Random Number to an Array in Java? - GeeksforGeeks
July 23, 2025 - ... // Java Program to Add Random ... main(String[] args) { int n = 5; int[] arr = new int[n]; // Create a Random object Random random = new Random(); // Assign random values to the array for (int i = 0; i < n; i++) { // Generate a ...
🌐
Processing Forum
forum.processing.org › topic › how-to-pick-random-value-from-array
How to pick random value from array? - Processing Forum
January 20, 2020 - Just replace fillColor w/ any array which has been already properly initialized, like those R, G & B: int rand = (int) random(R.length); I believe all of them share a characteristic of having 4 elements each! ... I reformatted the code worked to get rid of the error which made sense, but now the counter isn't working and the master circle fill color is always black and doesn't seem to be a random fill color anymore?
🌐
Quora
quora.com › How-can-I-randomize-an-array-in-Java
How to randomize an array in Java - Quora
If the array is already filled I can see two methods ( assuming you do not want to use the shuffle() method on type List 1. Create an ArrayList of objects of the type in the array.
🌐
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. ... 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); } }