-2

I'm new to C# so don't blame me for my stupidity. I'm working on an application that should randomize a word and give it's lenght for example you write a word "Line" and it gives you "iLen". Currently I'm sure that only this part works:

    private void lenght_Click(object sender, EventArgs e)
    {
        String word = textBox1.Text;
        int x = word.Length;
        MessageBox.Show(x.ToString());
    }

    private void randomize_Click(object sender, EventArgs e)
    {
        String word = textBox1.Text;
        int x = word.Length;


    }

I tried a lot but most of it just crashed the application so at the moment I would like to know what does the Text.ToCharArray does and I would love additional support. So I just need a method that takes your string randomizes it gives you another string just with mashed/randomized characters. Now I will leave the question for 5 - 7 hours to get more answers later I will review them all and give rep to the working ones. Thank you for support! I have read all your reviews yet I'm late sorry for that now it's time to check everything.

Aras
  • 89
  • 1
  • 1
  • 8
  • 1
    So far you have found how to show the length of a string in a message box. What have you tried to randomize the word? Have you checked the docs to see what `ToCharArray` does? Have you done any kind of research into this problem? – Daniel Kelley Sep 07 '13 at 13:10
  • 5
    _I'm new to C# so don't blame the noob._ We never do that! – Soner Gönül Sep 07 '13 at 13:10
  • 1
    You can use the same algorithm as described [here](http://stackoverflow.com/q/273313/335858) for a list. Convert your string to a list of chars, run the shuffle, and convert the result back to a string. – Sergey Kalinichenko Sep 07 '13 at 13:10

2 Answers2

4

Randomize method (from another SO question):

public static T[] Randomize<T>(T[] source)
{
    List<T> randomized = new List<T>();
    List<T> original = new List<T>(source);
    Random r = new Random();
    for (int size = original.Count; size > 0; size--)
    {
        int index = r.Next(size);
        randomized.Add(original[index]);
        original[index] = original[size - 1];
    }
    return randomized.ToArray();
}

And usage:

string text = "Line";

string randomized = new string(Randomize(text.ToCharArray()));
Community
  • 1
  • 1
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • 3
    And you can make an good extension method from this code. – Nemanja Boric Sep 07 '13 at 13:14
  • 1
    That's going to be hugely inefficient for large lists, because `RemoveAt` is an O(n) operation. If the array is even moderately sized, the resulting O(n^2) algorithm will take a really long time. You're better off just creating a single `List` and using a [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) – Jim Mischel Sep 07 '13 at 13:42
  • @JimMischel I've updated my answer. Should be better now. – MarcinJuraszek Sep 07 '13 at 13:49
  • 1
    That works, but you could do it without the second list. Just modify the `original` list in place (swap items) and return it. – Jim Mischel Sep 08 '13 at 21:25
  • @JimMischel To add to Jim's comment, if you don't want to mutate the original then copy it *once* to a new array. As it is you copy the array to a list, then copy that list to another list, then copy that list to a new array. Also note if you are going to copy the input into an array might as well accept it as an `IEnumerable`, to make things easier on the caller. Only one copy is needed, not three. Also note the proper term for this is "shuffling" rather than "randomize". – Servy Sep 10 '13 at 17:23
1
Random rand = new Random();
var output = new string(input.OrderBy(x => rand.Next()).ToArray());
King King
  • 61,710
  • 16
  • 105
  • 130