[英]Generating random words in Java?
I wrote up a program that can sort words and determine any anagrams.我编写了一个程序,可以对单词进行排序并确定任何字谜。 I want to generate an array of random strings so that I can test my method's runtime.我想生成一个随机字符串数组,以便我可以测试我的方法的运行时。
public static String[] generateRandomWords(int numberOfWords){
String[] randomStrings = new String[numberOfWords];
Random random = Random();
return null;
}
(method stub) (方法存根)
I just want lowercase words of length 1-10.我只想要长度为 1-10 的小写单词。 I read something about generating random numbers, then casting to char or something, but I didn't totally understand.我读了一些关于生成随机数,然后转换为字符或其他内容的内容,但我并不完全理解。 If someone can show me how to generate random words, then I should easily be able to just use a for loop to insert the words into the array.如果有人可以告诉我如何生成随机单词,那么我应该可以轻松地使用 for 循环将单词插入到数组中。 Thanks!谢谢!
Do you need actual English words, or just random strings that only contain letters az?您需要实际的英语单词,还是只包含字母 az 的随机字符串?
If you need actual English words, the only way to do it is to use a dictionary, and select words from it at random.如果您需要实际的英语单词,唯一的方法是使用字典,并从中随机选择单词。
If you don't need English words, then something like this will do:如果你不需要英语单词,那么像这样的事情会做:
public static String[] generateRandomWords(int numberOfWords)
{
String[] randomStrings = new String[numberOfWords];
Random random = new Random();
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);
}
return randomStrings;
}
来自 commons-lang 的RandomStringUtils
If you want to generate random words of a given length, you'll either need an algorithm to determine if a given string is a word (hard), or access to a word list of all the words in a given language (easy).如果您想生成给定长度的随机单词,您要么需要一种算法来确定给定的字符串是否为单词(难),要么需要访问给定语言中所有单词的单词列表(简单)。 If it helps, here's a list of every word in the Scrabble dictionary .如果有帮助, 这里是 Scrabble 词典中每个单词的列表。
Once you have a list of all words in a language, you can load those words into an ArrayList
or other linear structure.一旦你有了语言中所有单词的列表,你就可以将这些单词加载到ArrayList
或其他线性结构中。 You can then generate a random index into that list to get the random word.然后,您可以在该列表中生成一个随机索引以获取随机单词。
Why generating random words?为什么要生成随机词? When you can use some dictionaries .什么时候可以用一些词典。
You can call this method for each word you want to generate.您可以为要生成的每个单词调用此方法。 Note that the probability of generating anagrams should be relatively low though.请注意,生成字谜的概率应该相对较低。
String generateRandomWord(int wordLength) {
Random r = new Random(); // Intialize a Random Number Generator with SysTime as the seed
StringBuilder sb = new StringBuilder(wordLength);
for(int i = 0; i < wordLength; i++) { // For each letter in the word
char tmp = 'a' + r.nextInt('z' - 'a'); // Generate a letter between a and z
sb.append(tmp); // Add it to the String
}
return sb.toString();
}
If you want random words without using a dictionary...如果你想要不使用字典的随机单词......
Repeat these steps for the number of words you want to generate.对要生成的单词数重复这些步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.