Use the Random.nextInt(int) method:

final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};
Random random = new Random();
int index = random.nextInt(proper_noun.length);
System.out.println(proper_noun[index]);

This code is not completely safe: one time out of four it'll choose Richard Nixon.

To quote a documentation Random.nextInt(int):

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

In your case passing an array length to the nextInt will do the trick - you'll get the random array index in the range [0; your_array.length)

Answer from Nigel Tufnel on Stack Overflow
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ generate array of random strings in java
Generate array of random strings in Java - CodeSpeedy
January 18, 2022 - Line number 9, using this for loop, we generate a string of random characters and the length of the string specified in stringLength. Line number 11, we get the number in the createdRandomChar variable and our code does not exceed the maximum value. it will generate a random number from 0 to maximum using the randomObj.nextInt(maximum) method.
๐ŸŒ
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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 397689 โ€บ java โ€บ creating-random-strings-array
help with creating random strings in an array (Beginning Java forum at Coderanch)
November 10, 2004 - I'll give you a piece of code: letter = alphabet[r.nextInt(alphabet.length)] nextInt(x) returns an integer >= 0 and <x This gives you one random letter. You can assemble a String from letters using StringBuffer, you can easily make StringBuffers into Strings with a String constructor, and you can put references to your Strings in a String[] array.
๐ŸŒ
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)
December 23, 2011 - My desired output would be: random String, random String, random String, random String another ex: Plates, Silver Coins, Dressing Table, Candy Wrapper, Chipped Bat Please note that I want all of the randomly picked strings stored in ONE variable. EDIT: solved it. My idiotic brain did not realize to just add the below code in the for loop. ... Sorry for the double post, and the useless thread, but I solved it . ... Boost this thread! ... Randomly shuffling an array.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 42320732 โ€บ generating-a-random-string-array
java - Generating a Random String Array - Stack Overflow
You can do it using nested loop, as you said in the question: public String[] randomArrayString(int length, int numberOfChar) { Random random = new Random(); char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray(); String[] array = new ...
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
September 25, 2019 - 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 ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ generate-a-random-string-in-java
Generate a random string in Java
import java.util.Random; public class Demo { public static void main(String[] args) { String[] strArr = { "P", "Q", "R", "S","T", "U", "V", "W" }; Random rand = new Random(); int res = rand.nextInt(strArr.length); System.out.println("Displaying a random string = " + strArr[res]); } }
๐ŸŒ
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 } }
๐ŸŒ
SitePoint
sitepoint.com โ€บ get started
Random String from Array - Get Started - SitePoint Forums | Web Development & Design Community
August 30, 2014 - How can I now return a random string from the following array? import java.util.Random; public class RpsBW { public void setObjects() { String objects[] = {"Rock", "Paper", "Scissors"}; } public sโ€ฆ
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java โ€“ generate random string
Java - Generate Random String | Baeldung
May 11, 2024 - Learn how to generate random numbers in Java - both unbounded as well as within a given interval. ... Learn how the JVM optimizes the amount of memory allocated to String storage in the Java String Pool. ... @Test public void givenUsingPlainJava_whenGeneratingRandomStringUnbounded_thenCorrect() { byte[] array = new byte[7]; // length is bounded by 7 new Random().nextBytes(array); String generatedString = new String(array, Charset.forName("UTF-8")); System.out.println(generatedString); }
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 27742052 โ€บ randomising-strings-in-an-arrayjava
Randomising strings in an array(Java) - Stack Overflow
How to generate random words from an array of strings? e.g. ... I know the math.Random method and Random class operate on int,boolean and float values and i'm guessing i'll need the array index in my implementation. ... random words with meaning? or just random permutations of the words? please provide an output example. ... Have a look at Arrays.asList and Collections.shuffle... ... import java.security.SecureRandom; public final class SessionIdentifierGenerator { private SecureRandom random = new SecureRandom(); public String nextSessionId() { return new BigInteger(130, random).toString(32); } }
๐ŸŒ
Narkive
programming.comp.narkive.com โ€บ YK1cYpiT โ€บ picking-a-random-string-from-an-array-in-java
Picking a random string from an array in Java?
For what it's worth: String myStrings[] = // whatever String randomStr = myStrings[ (int) (Math.random() * myStrings.length) ]; That's the proper way. Math.random() always returns a value strictly less than 1, so when multiplied by myStrings.length and truncated to an int, it will be a random ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1654206 โ€บ please-help-me-random-string-in-java
(Please help me) random string in java | Sololearn: Learn to code for FREE!
Create your 2 arrays, an empty one and the one with the strings in order, use a loop and in the loop make a random number, check if array[rndnum] is null, if so store it into the array eg: second[rndnum] = first[x] if it's not true, you want ...