Shuffle a generic array - CSharp System

CSharp examples for System:Array Shuffle

Description

Shuffle a generic array

Demo Code


using System.Collections.Generic;
using System;/*from ww w  .  ja v a2  s  .  co  m*/

public class Main{
    public static T[] Shuffle<T>(this T[] array)
      {
         var r = new Random();
         for (int i = array.Length; i > 0; i--)
         {
            int j = r.Next(i);
            T k = array[j];
            array[j] = array[i - 1];
            array[i - 1] = k;
         }
         return array;
      }
}

Related Tutorials