Mr_CryptoPrime
Mr_CryptoPrime

Reputation: 638

Generating random words in Java?

I wrote up a program that can sort words and determine any anagrams. I want to generate an array of random strings so that I can test my method's runtime.

public static String[] generateRandomWords(int numberOfWords){
String[] randomStrings = new String[numberOfWords];
Random random = Random();
    return null;
}

(method stub)

I just want lowercase words of length 1-10. I read something about generating random numbers, then casting to char or something, but I didn't totally understand. If someone can show me how to generate random words, then I should easily be able to just use a for loop to insert the words into the array. Thanks!

Upvotes: 14

Views: 99449

Answers (7)

guest3423423
guest3423423

Reputation: 1

 public static void main(final String[] args) throws IOException {
        final var dict   = getDictionary();
        final var random = ThreadLocalRandom.current();
        final var word   = dict.get(random.nextInt(0, dict.size()));
        System.out.println(word);
    }

    private static List<String> getDictionary() throws IOException {
        Path filePath = Paths.get("/usr/share/dict/words");
        return Files.readAllLines(filePath);
    }

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 372664

If you want to generate random words of a given length, you'll either need an algorithm to determine if a given string is a word (hard), or access to a word list of all the words in a given language (easy). If it helps, here's a list of every word in the Scrabble dictionary.

Once you have a list of all words in a language, you can load those words into an ArrayList or other linear structure. You can then generate a random index into that list to get the random word.

Upvotes: 4

Yanick Rochon
Yanick Rochon

Reputation: 53521

Why generating random words? When you can use some dictionaries.

Upvotes: 5

Aravind Yarram
Aravind Yarram

Reputation: 80166

RandomStringUtils from commons-lang

Upvotes: 13

David Yaw
David Yaw

Reputation: 27864

Do you need actual English words, or just random strings that only contain letters a-z?

If you need actual English words, the only way to do it is to use a dictionary, and select words from it at random.

If you don't need English words, then something like this will do:

public static String[] generateRandomWords(int numberOfWords)
{
    String[] randomStrings = new String[numberOfWords];
    Random random = new Random();
    for(int i = 0; i < numberOfWords; i++)
    {
        char[] word = new char[random.nextInt(8)+3]; // words of length 3 through 10. (1 and 2 letter words are boring.)
        for(int j = 0; j < word.length; j++)
        {
            word[j] = (char)('a' + random.nextInt(26));
        }
        randomStrings[i] = new String(word);
    }
    return randomStrings;
}

Upvotes: 25

Argote
Argote

Reputation: 2155

You can call this method for each word you want to generate. Note that the probability of generating anagrams should be relatively low though.

String generateRandomWord(int wordLength) {
    Random r = new Random(); // Intialize a Random Number Generator with SysTime as the seed
    StringBuilder sb = new StringBuilder(wordLength);
    for(int i = 0; i < wordLength; i++) { // For each letter in the word
        char tmp = 'a' + r.nextInt('z' - 'a'); // Generate a letter between a and z
        sb.append(tmp); // Add it to the String
    }
    return sb.toString();
}

Upvotes: 2

Kin
Kin

Reputation: 1465

If you want random words without using a dictionary...

  1. Make a list of all the letters you want possible in your words
  2. Generate a random index to pick out a letter from the list
  3. Repeat until you have your desired word length

Repeat these steps for the number of words you want to generate.

Upvotes: 1

Related Questions