gpt4 book ai didi

java - 有没有更好的方法让我的计算机对手生成 (3 > 随机长度 <= 7) 的单词?

转载 作者:行者123 更新时间:2023-11-30 05:24:32 24 4
gpt4 key购买 nike

我已经实现了以下方法来执行此操作,但我仍然发现很多时候它只生成三个字母的单词。有时,它确实会出现四个字母、五个字母等单词,但大多数仍然是三个字母。有没有其他方法可以保证真正的随机性,使其始终变化且从不重复?如果我需要进一步澄清,请告诉我。这是一个类似拼字游戏的游戏,我正在其中构建 Java 编程类(class)的最终项目。 TIA!

这是我的代码:

//METHOD - getWordFromComputer
public static String getWordFromComputer(char[] let)
{
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
String word = "";
boolean done = false;
while(!done)
{
word = "";
boolean[] used = new boolean[7];
int lettersUsed = 0;
int max = 7;
int min = 3;
int size = rand.nextInt((max - min) + 1) + min;//Code provided by: mkyong.com
while(lettersUsed < size)
{
int i = rand.nextInt(7); //Random from 0 to 6 (7)
if(!used[i]) { //The current index hasn't been chosen yet
used[i] = true;
lettersUsed++;
if(let[i] != '_') {
word += let[i];
} else {
word += letters[rand.nextInt(26)];
}
}
}
if(wordVerify(word)) {
done = true;
}
}
return word;
}

最佳答案

您的代码的问题在于您正在生成随机长度的单词,并尝试它是否有效。短单词有效的概率要高得多。解决方案是生成一次长度,然后进入 while 循环尝试不同的单词,直到其中一个有效。

关于java - 有没有更好的方法让我的计算机对手生成 (3 > 随机长度 <= 7) 的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58929428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com