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
๐ŸŒ
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.
Discussions

java - Random element from string array - Stack Overflow
-2 How do you make the computer select random elements from an array? More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 30, 2017
Random String from Array - Get Started - SitePoint Forums | Web Development & Design Community
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 static void main(String args[]) { // main } } It would be decent to post an example, thanks! ๐Ÿ˜ƒ More on sitepoint.com
๐ŸŒ sitepoint.com
0
August 30, 2014
java - How to use method to output a random string from array? - Stack Overflow
How can I fix the code so my method compiles correctly to output a random string from the array I created into the main method? class Main { public static void main (String[] args) { More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to get a random string from an array of strings? - Stack Overflow
I need random strings out of array S with the method get(), but I really don't know how to do it. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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]); } }
๐ŸŒ
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 ...
๐ŸŒ
Dirask
dirask.com โ€บ posts โ€บ Java-pick-random-string-from-array-of-strings-y1xln1
Java - pick random string from array of strings
As nextInt function is called we ... JavaRandomStringFromArray { public static String randomStringFromArr() { String[] arr = {"A", "B", "C", "D", "E", "F"}; int randIdx = ThreadLocalRandom.current().nextInt(arr.length); String ...
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ generate array of random strings in java
Generate array of random strings in Java - CodeSpeedy
January 18, 2022 - Line number 13, using an enhance-for loop to print our random strings from the array. Generated array of Random strings are ============================= 4bZXMOSB8YmDyvmjUjpAMIXOl 96Ep3i110hqQ2cIUtotp5OGS8 IvqusF8TA31OMSxqEZK3MG7kj 7xkEfICq9pqr8sbmUYRtICP9x rInOc3BRYfVcNjCCCVx5JRSGg ============================= Also read: Java Program to Generate Array of Random Integers
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java โ€“ generate random string
Java - Generate Random String | Baeldung
May 11, 2024 - ... @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")); ...
Find elsewhere
๐ŸŒ
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โ€ฆ
๐ŸŒ
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 ...
๐ŸŒ
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 - 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 ...
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ random-string-from-string-array-list
How to Pick a Random String from a String Array in Java: Example with Country List โ€” javathinking.com
Or perhaps youโ€™re creating a ... string from a string array is a common task in Java. In this blog, weโ€™ll explore **three reliable methods** to select a random string from a string array, using a practical example with a list of countries....
๐ŸŒ
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 ...
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to print random string from an array?
r/javahelp on Reddit: How to print random String from an Array?
October 20, 2016 -

I am a beginner when it comes to Java, but I am trying to create a program that will output a complete Fantasy Football lineup at random.

My first step was to create an ArrayList of all the teams in the NFL (not sure if this was the most appropriate approach).

With the ArrayList filled, how to I then print a random String from it? In other words, I would like the program (at this point) to simply output a random NFL team. I realize that this is not a complete fantasy lineup.

http://pastebin.com/181vJPKE

Edit: I am not sure how to embed from pastebin. Help?

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 27742052 โ€บ randomising-strings-in-an-arrayjava
Randomising strings in an array(Java) - Stack Overflow
... import java.security.SecureRandom; public final class SessionIdentifierGenerator { private SecureRandom random = new SecureRandom(); public String nextSessionId() { return new BigInteger(130, random).toString(32); } }