Shuffle element in IList - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Shuffle element in IList

Demo Code


using System.Collections.Generic;
using System;/*w ww .  j  av a  2  s . c  om*/

public class Main{
        public static void Shuffle<T>(this IList<T> list)
        {
            var rng = new Random();
            var n = list.Count;
            while (n > 1)
            {
                n--;
                var k = rng.Next(n + 1);
                var value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
}

Related Tutorials