Last active
August 29, 2015 14:04
-
-
Save XinyueZ/3acc100137090abbeccc to your computer and use it in GitHub Desktop.
generate random words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* http://stackoverflow.com/a/4952066/1835650 | |
*/ | |
public static String[] generateRandomWords(int _numberOfWords) { | |
String[] randomStrings = null; | |
Random random = null; | |
try { | |
random = new Random(); | |
randomStrings = new String[_numberOfWords]; | |
for (int i = 0; i < _numberOfWords; i++) { | |
char[] word = new char[random.nextInt(8) + 3]; // words of | |
// length 3 | |
// through 10. | |
// (1 and 2 | |
// letter words | |
// are boring.) | |
for (int j = 0; j < word.length; j++) { | |
word[j] = (char) ('a' + random.nextInt(26)); | |
} | |
randomStrings[i] = new String(word); | |
} | |
} catch (Exception _e) { | |
randomStrings = null; | |
Log.e(TAG, "Error in generateRandomWords: " + _e.getMessage()); | |
} finally { | |
random = null; | |
} | |
return randomStrings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment