Shuffle elements in random order. To get a random on the order used Random class. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Shuffle elements in random order. To get a random on the order used Random class.

Demo Code


using System.Security.Cryptography;
using System.Linq;
using System.Collections.Generic;
using System;/*from ww  w  .j a va  2  s . c om*/

public class Main{
        /// <summary>
        /// Shuffle elements in random order. To get a random on the order used Random class.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static IEnumerable<T> FastShuffle<T>(this IEnumerable<T> source)
        {
            var rnd = new Random();
            return source.OrderBy(item => rnd.Next());
        }
}

Related Tutorials