Does Fisher Yates shuffle on one line against using permutations of the same length?

Now I am working on a set of game games as a learning tool (and to recreate some of my favorite word games!) With the help of the "actual" learned friend-programmer, we implemented a good permutation method in one of my classes. He finds all permutations of 3 letters or more and compares them with lists of strings, which contain, in fact, a list of words from the Scrabble tournament.

Here is the background, here is my current problem: now I have all the permutations and compare them with existing words and created a new List with all possible combinations of words on this line. However, when I present this line to the user, I need it to be scrambled. I found several C # implementations in Shuffle from Fisher-Yates, but I was not able to adapt them to accept a single line (EDIT problem: Fisher-Yates resolved using the char [] array). Then I had the idea to hack a bit - why not use one of the permutations with the same length, but this! = Source word.

Unfortunately, every time my conditional statement returns a word back. Not so difficult for the end user to figure out :) Here is my code for scrambling:

// permWords is a Dictionary<int, List<string>>
String strScrambled= "";

        foreach (List<string> listWords in permWords.Values)
        {
            foreach (string word in listWords)
            {
                if (word.Length == strWord.Length && word != strWord)
                {
                    strScrambled = word;
                }

            }
        }

strScrambled = word + 1, , . , "" ; , - .

, Fisher-Yates char, , - , , - , . , ; .

+2
2

, , :

void FisherYatesShuffle(char[] elements)
{
    int N = elements.Count;
    for(int i = 0; i<N-1; i++)
    {
        // exchange elements[i] with a random element in i+1 .. N
    }   
}

, , CharArray :

string shuffle(string input)
{
    var arr = input.ToCharArray();
    FisherYatesShuffle(arr);
    return new String(arr);
}
+3

, ...

string str = "hello";

// The random number sequence
Random num = new Random();

string rand = str;
while (rand == str)
    rand = new string(str.OrderBy(s => (num.Next())).ToArray());

, , while...

while (rand == str || rand == str.Reverse())
+1

All Articles