Java Random String generateRandomString(int wordsLength, String separator)

Here you can find the source of generateRandomString(int wordsLength, String separator)

Description

Generates a string of random words with the supplied separator

License

Open Source License

Parameter

Parameter Description
wordsLength a parameter

Declaration

public static String generateRandomString(int wordsLength, String separator) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {
    private static String[] words = new String[] { "cogitation", "unwilful", "intendment", "flexibleness",
            "intervertebrally", "magi", "phalanx", "pipsqueak", "bosnian", "martinmas", "vimana", "agitational",
            "hyperorthognathous", "hierarchize", "cheshunt", "trachea", "consternate", "deep", "calcimined",
            "sublunated", "sorer", "interlaminar", "polemicist", "overassertive", "endolymph", "dichloride",
            "simultaneously", "prerejoicing", "degradedness", "hexameron", "consecratedness", "nondisciplining",
            "bimorph", "planck", "somewise", "milfoil", "postprandially", "isolate", "pseudoerotic", "fishiness",
            "benefiter", "kola", "socialist", "frenchify", "unglorifying", "innovating", "sotol", "downcastly",
            "wkly", "unabolished", "cogitation", "unwilful", "intendment", "flexibleness", "intervertebrally",
            "magi", "phalanx", "pipsqueak", "bosnian", "martinmas", "vimana", "agitational", "hyperorthognathous",
            "hierarchize", "cheshunt", "trachea", "consternate", "deep", "calcimined", "sublunated", "sorer",
            "interlaminar", "polemicist", "overassertive", "endolymph", "dichloride", "simultaneously",
            "prerejoicing", "degradedness", "hexameron", "consecratedness", "nondisciplining", "bimorph", "planck",
            "somewise", "milfoil", "postprandially", "isolate", "pseudoerotic", "fishiness", "benefiter", "kola",
            "socialist", "frenchify", "unglorifying", "innovating", "sotol", "downcastly", "wkly", "unabolished" };

    /**/*  w  ww .jav a 2 s . c o  m*/
     * Generates a string of random words with the supplied separator
     * @param wordsLength
     * @return
     */
    public static String generateRandomString(int wordsLength, String separator) {

        Random rand = new Random();
        String sb = "";

        for (int i = 0; i < wordsLength; i++) {
            int randomWord = rand.nextInt(words.length);

            if (i != 0) {
                sb += separator;
            }

            sb += words[randomWord];
        }

        return sb;
    }
}

Related

  1. generateRandomString(int minLength, int maxLength, int minLCaseCount, int minUCaseCount, int minNumCount, int minSpecialCount)
  2. generateRandomString(int n)
  3. generateRandomString(int n)
  4. generateRandomString(int size)
  5. generateRandomString(int stringLength)
  6. generateRandomString(Random random, int length)
  7. generateRandomString(Random random, int size)
  8. generateRandomString(Random rnd, char[] alphabet, int maxLength)
  9. generateRandomString(String str, int length)