/// <summary> /// Creates a shallow copy of the enumeration, scrambles and returns it. /// </summary> public static IEnumerable <T> Scramble <T>(this IEnumerable <T> items) { var itemsCopy = new List <T>(items); for (var i = 0; i < itemsCopy.Count; i++) { var randomIndex = Randomness.GetRandomInt(0, itemsCopy.Count); var temp = itemsCopy[randomIndex]; itemsCopy[randomIndex] = itemsCopy[i]; itemsCopy[i] = temp; } return(itemsCopy); }
/// <summary> /// Returns null if the enumeration has no elements. /// </summary> public static T GetRandomElement <T>(this IEnumerable <T> enumeration) { return(enumeration.ElementAtOrDefault(Randomness.GetRandomInt(0, enumeration.Count()))); }