get Random Words - Java java.util

Java examples for java.util:Random

Description

get Random Words

Demo Code


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Main{
    public static String getRandomWords(int wordCount) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < wordCount; i++) {
            if (i > 0) {
                buf.append(" ");
            }// w  ww  .j ava  2 s.  c om
            String word = RandomDataUtil
                    .getRandomAlphaString(RandomDataUtil
                            .getRandomInt(5, 15));
            if (i == 0) {
                word = Character.toUpperCase(word.charAt(0))
                        + word.substring(1).toLowerCase();
            } else {
                word = word.toLowerCase();
            }
            buf.append(word);
        }
        return buf.toString();
    }
    public static String getRandomWords(int min, int max) {
        int wordsToCreate = getRandomInt(min, max);
        return getRandomWords(wordsToCreate);
    }
    public static String getRandomAlphaString(int length) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < length; i++) {
            //   ascii 60 to 90
            char str = (char) (65 + (int) (Math.random() * (90 - 65)));
            b.append(str);
        }

        return b.toString();
    }
    public static int getRandomInt() {
        return getRandomInt(1, Integer.MAX_VALUE);
    }
    public static int getRandomInt(int min, int max) {
        return min + (int) ((Math.random() * (max - min)));
    }
}

Related Tutorials