I have implemented the following method to do as such, but I still find that a lot of the times it produces only a three letter word. Once in a great while, it does do a four letter, five letter, etc. word, but it is still most three letters. Is there any other way that can guarantee a true random, to where it's always changing and never repetitive? Please let me know if I need to clarify myself further. This is for a Scrabble-like game, in which I am building for a final project in my Java programming class. TIA!
Here is my code:
//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;
}
Aucun commentaire:
Enregistrer un commentaire