Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is my solution for the intermediate level challenge #95
- on reddit.com/r/dailyprogrammer
- Created by Robert Allen (Unh0ly_Tigg)
- */
- import java.util.Random;
- public class FillerText {
- public static final int maxNumWordsPerSentence = 8;
- public static final int minNumWordsPerSentence = 3;
- public static final int minNumCharsPerWord = 1;
- public static final int maxNumCharsPerWord = 12;
- public static final boolean capitalizeSentence = true;
- public static final boolean endWithPeriods = true;
- public static final float lineBreakChance = 0.15f;
- public static final float paragraphLineBreakChance = 0.50f;
- private static final String validGenChars = "abcdefghijklmnopqrstuvwxyz";
- private Random random = new Random();
- public String randomWord(int numChars) {
- String s = "";
- for(int i = 0; i < numChars; i++)
- s += validGenChars.charAt(random.nextInt(validGenChars.length()));
- return s;
- }
- public int getRandomInt(int lower, int upper) {
- return random.nextInt(upper - lower) + lower;
- }
- public String randomSentence(int numWords) {
- String s = "";
- for(int i = 0; i < numWords; i++) {
- s += randomWord(getRandomInt(minNumCharsPerWord, maxNumCharsPerWord));
- if(i != (numWords - 1))
- s += " ";
- }
- if(capitalizeSentence)
- // get first letter get rest of sentence
- s = s.substring(0, 1).toUpperCase() + s.substring(1);
- if(endWithPeriods)
- s += ".";
- return s;
- }
- public String getLineBreak() {
- String s = " ";
- if(getRandomInt(0, 100) < (100 * lineBreakChance)) {
- s += "\n";
- if(getRandomInt(0, 100) < (100 * paragraphLineBreakChance))
- s += "\n";
- }
- return s;
- }
- public static void main(String[] args) {
- FillerText filler = new FillerText();
- System.out.println(filler.randomFiller(1000));
- }
- public String randomFiller(int numWords) {
- int currentCount = 0;
- String s = "";
- while(currentCount < numWords) {
- int wordCount = getRandomInt(minNumWordsPerSentence, maxNumWordsPerSentence);
- s += randomSentence(wordCount) + getLineBreak();
- currentCount += wordCount;
- }
- return s;
- }
- }
Add Comment
Please, Sign In to add comment