/// <summary> /// Chooses a random element out of a IList collection and returns the result /// </summary> /// <returns>The randomly selected item</returns> /// <param name="source">Source list to choose for items</param> /// <typeparam name="T">The type of items in the list</typeparam> public static T ChooseOne <T>(this IList <T> source) { if (source == null || source.Count == 0) { throw new ArgumentException("Cannot choose from an empty list"); } var index = Randomly.Range(0, source.Count); return(source[index]); }
/// <summary> /// Chooses a random element out of a IList collection and returns the result /// </summary> /// <returns>The randomly selected item</returns> /// <param name="source">Source list to choose for items</param> /// <typeparam name="T">The type of items in the list</typeparam> public static T ChooseOne <T>(this IEnumerable <T> source) { if (source == null || source.Count() == 0) { throw new ArgumentException("Cannot choose from an empty list"); } var index = Randomly.Range(0, source.Count()); return(source.ElementAt(index)); }
/// <summary> /// Chooses a random element out of the array and returns the result /// </summary> /// <returns>The randomly selected item</returns> /// <param name="source">Source array to choose from the array</param> /// <typeparam name="T">Type of elements in the array</typeparam> public static T ChooseOne <T>(this T[] source) { var index = Randomly.Range(0, source.Length); return(source[index]); }
public T ChooseRandomly() { var value = Randomly.Range(1, maxValue); return(GetOption(value)); }