I'm trying to scramble words(strings) randomaly but some of them left as they were in the original w

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the code im using:private void GetText()
{
_lengthaboveone = new List<string>();
for (int i = 0; i < _words.Count; i++)
{
string word = _words;
if (word.Length < 4) continue;

string first = word.Substring(0, 1);
string last = word.Substring(word.Length - 1, 1);
string middle = word.Substring(1, word.Length - 2);
_lengthaboveone.Add(middle);
_words = first + MakeRandomwords(middle) + last;//MakeRandomwords(middle) + last;
}
_scrambledWords = _words;
}

_words is List<string>
Then the scramble function:private static StringBuilder MakeRandomwords(string theWord)
{
var jumbleSb = new StringBuilder();
jumbleSb.Append(theWord);
int lengthSb = jumbleSb.Length;
for (int i = 0; i < lengthSb; ++i)
{
int index1 = (RandomGen.Next() % lengthSb);
int index2 = (RandomGen.Next() % lengthSb);

Char temp = jumbleSb[index1];
jumbleSb[index1] = jumbleSb[index2];
jumbleSb[index2] = temp;
}

return jumbleSb;
}

In the end the List<string> _scrambledWords is filled with more then 1000 indexs in each one a string(word)
Most of them are scrambled randomaly in the middle .
But some of the words are the same as they was in the original nothing changed.
Something with the MakeRandomwords function is not working good enough.
Maybe i need somehow to make it recursive and compare each scrambled word to the original word so if its not scrambled to keep loop untill the word is scrambled ?
Or the function it self the random scramble code is not good enough.

View the full article
 
Back
Top