Shuffle IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Shuffle IEnumerable

Demo Code


using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public class Main{
        public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
        {//  ww w.  java2s  .c  om
            var array = source.ToArray();
            array.Shuffle();
            return array;
        }
        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
}

Related Tutorials