Skip to content

Instantly share code, notes, and snippets.

@benhardy
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhardy/9817542 to your computer and use it in GitHub Desktop.
Save benhardy/9817542 to your computer and use it in GitHub Desktop.
public List<String> randomDictionaryWords() throws IOException {
try (InputStream is = new FileInputStream(new File("/usr/share/dict/words"))) {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
return br.lines()
.filter(line -> line.length() >= MIN_WORD_LENGTH && line.length() <= MAX_WORD_LENGTH)
.filter(word -> word.equals(word.toLowerCase()))
.filter(word -> random() < DICTIONARY_SAMPLING_RATE)
.limit(50)
.collect(Collectors.toList());
}
}
private String createRandomWord(List<String> dictionary) {
return IntStream.range(0, randomInt(MAX_WORDS_PER_PHRASE) + 1)
.mapToObj(i -> dictionary.get(randomInt(dictionary.size())))
.collect(Collectors.joining("-"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment