mercredi 29 août 2018

How to Find All Possible Anagrams?

So I want to make an anagram decoder, and I want to know if there's a better way to do it. My current code is this:

Random random = new Random();
char[] splitAnagram = chosenAnagram.toCharArray();
int[] presetNumbers = new int[chosenAnagram.length()];
int i = 0;
while (true) {
    TimeUnit.MILLISECONDS.sleep(10);
    int thisRandom = random.nextInt(chosenAnagram.length());
    if (Arrays.asList(presetNumbers).contains(thisRandom) == true)
        presetNumbers[i] = thisRandom;
    else
        continue;
    i++;
    if (i > chosenAnagram.length())
        break;
}
for (int l = 0; l < chosenAnagram.length(); l++) {
    System.out.println(splitAnagram[presetNumbers[l]]);
}

Basically, it generates random numbers for each letter, so that it "scrambles" the word. It's flawed as is, because it's not even supposed to loop yet, just make one scramble, but it just in-general seems really slow and prone-to-error (I sleep by 10ms because it can only make random numbers every system time change).

I'm wondering if there's some other algorithm for this, or a possible API I could use. Thank you!

P.S I'm running Eclipse Photon, in case you know of a plugin for that.




Aucun commentaire:

Enregistrer un commentaire