/// <summary> /// Gets a random element for the IEnumerable /// </summary> /// <typeparam name="T">Type of source IEnumerable</typeparam> /// <param name="source">Source IEnumerable collection</param> /// <returns>T.</returns> public static T GetRandomElement <T>(this IEnumerable <T> source) { // If the list is empty then return an empty instance of T if (source.IsEmpty()) { return(Activator.CreateInstance <T>()); } // Get a random number int randomNumber = RandomHelpers.Next(source.Count() - 1); return(source.ElementAt(randomNumber)); }
/// <summary> /// Shuffles the specified source. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">The source.</param> /// <returns>IEnumerable<T>.</returns> public static IEnumerable <T> Shuffle <T>(this IEnumerable <T> source) { var shuffled = source.OrderBy(src => RandomHelpers.Next(source.Count())); return(shuffled); }