Skip to content

Instantly share code, notes, and snippets.

@mattshapiro
Created September 15, 2015 04:49
Show Gist options
  • Save mattshapiro/f1d6ac095aac995d6e74 to your computer and use it in GitHub Desktop.
Save mattshapiro/f1d6ac095aac995d6e74 to your computer and use it in GitHub Desktop.
If you ever need to randomly generate fake "words" using english alphanumerics in Java...
private String getRandomizedWords(int word_count) {
String contentAsString = "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(word_count-- > 0) {
// generate word
int wordLength = Math.abs((rnd.nextInt() % MAX_WORD_LENGTH) + 1);
for(int i = 0; i < wordLength; i++) {
int val = Math.abs((rnd.nextInt() % 62) + 1);
char nextChar = ' ';
if (val >= 0 && val <= 9) {
nextChar = (char) (val + 48);
} else if (val >= 10 && val <= 35) {
nextChar = (char) (val + 55);
} else if (val >= 36 && val <= 62) {
nextChar = (char) (val + 61);
} else {
nextChar = (char) val;
}
baos.write(nextChar);
}
if(word_count != 0) {
baos.write(' ');
}
}
contentAsString = baos.toString();
try { baos.close(); } catch (Exception e) { e.printStackTrace(); }
return contentAsString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment