Unh0ly_Tigg

DailyProgrammer Challenge #95 intermediate level

Sep 3rd, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. /*
  2.   This is my solution for the intermediate level challenge #95
  3.   on reddit.com/r/dailyprogrammer
  4.   Created by Robert Allen (Unh0ly_Tigg)
  5. */
  6. import java.util.Random;
  7.  
  8. public class FillerText {
  9.     public static final int maxNumWordsPerSentence = 8;
  10.     public static final int minNumWordsPerSentence = 3;
  11.     public static final int minNumCharsPerWord = 1;
  12.     public static final int maxNumCharsPerWord = 12;
  13.     public static final boolean capitalizeSentence = true;
  14.     public static final boolean endWithPeriods = true;
  15.     public static final float lineBreakChance = 0.15f;
  16.     public static final float paragraphLineBreakChance = 0.50f;
  17.  
  18.     private static final String validGenChars = "abcdefghijklmnopqrstuvwxyz";
  19.     private Random random = new Random();
  20.     public String randomWord(int numChars) {
  21.         String s = "";
  22.         for(int i = 0; i < numChars; i++)
  23.             s += validGenChars.charAt(random.nextInt(validGenChars.length()));
  24.         return s;
  25.     }
  26.  
  27.     public int getRandomInt(int lower, int upper) {
  28.         return random.nextInt(upper - lower) + lower;
  29.     }
  30.  
  31.     public String randomSentence(int numWords) {
  32.         String s = "";
  33.         for(int i = 0; i < numWords; i++) {
  34.             s += randomWord(getRandomInt(minNumCharsPerWord, maxNumCharsPerWord));
  35.             if(i != (numWords - 1))
  36.                 s += " ";
  37.         }
  38.         if(capitalizeSentence)
  39.             //  get first letter                  get rest of sentence
  40.             s = s.substring(0, 1).toUpperCase() + s.substring(1);
  41.         if(endWithPeriods)
  42.             s += ".";
  43.         return s;
  44.     }
  45.  
  46.     public String getLineBreak() {
  47.         String s = " ";
  48.         if(getRandomInt(0, 100) < (100 * lineBreakChance)) {
  49.             s += "\n";
  50.             if(getRandomInt(0, 100) < (100 * paragraphLineBreakChance))
  51.                 s += "\n";
  52.         }
  53.         return s;
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         FillerText filler = new FillerText();
  58.         System.out.println(filler.randomFiller(1000));
  59.     }
  60.  
  61.     public String randomFiller(int numWords) {
  62.         int currentCount = 0;
  63.         String s = "";
  64.         while(currentCount < numWords) {
  65.             int wordCount = getRandomInt(minNumWordsPerSentence, maxNumWordsPerSentence);
  66.             s += randomSentence(wordCount) + getLineBreak();
  67.             currentCount += wordCount;
  68.         }
  69.         return s;
  70.     }
  71. }
Add Comment
Please, Sign In to add comment