Turkalp Kucur

Turkalp Kucur

  • NA
  • 35
  • 16k

method for shuffling elements of an array

Oct 29 2012 5:50 AM
Hello my friends.

I need a method which takes an array of element than returns it with elements which are in randomized order.

For that purpose I developed following method

        string[] shuffle(string[] a)

             for (int i = 0; i < a.Length; i++)
                 string tmp = a[i];
                 int randomnumber = rnd.Next(i, a.Length);
                 a[i] = a[randomnumber];
                 a[randomnumber] = tmp;

             return a;
but it returns sometimes the duplicate elements, which is wrong.

I need help.

Answers (2)