示例#1
0
        /// <summary>Given a list interface of items, return a random item from the List.</summary>
        /// <typeparam name="T">The type of the item in <paramref name="listToGetFrom" />.</typeparam>
        /// <param name="listToGetFrom">The list to extract a random item from.</param>
        /// <returns>The randomized item to return from the list.</returns>
        public static T GetRandomItem <T>(this IList <T> listToGetFrom)
        {
            int index = NumberRandomizer.Next(0, listToGetFrom.Count);

            if (listToGetFrom.HasIndex(index))
            {
                return(listToGetFrom[index]);
            }

            throw new IndexOutOfRangeException("We tried to get an item from a list at an index" + "that was" +
                                               "out of range!");
        }
示例#2
0
        /// <summary>Given a List, shuffle and return it.</summary>
        /// <typeparam name="T">The type of object within the given list.</typeparam>
        /// <param name="listToShuffle">The list we are to shuffle before returning.</param>
        /// <returns>The newly shuffled list.</returns>
        public static IList <T> Shuffle <T>(this IList <T> listToShuffle)
        {
            int n = listToShuffle.Count;

            while (n > 1)
            {
                n--;
                int k     = NumberRandomizer.Next(n + 1);
                T   value = listToShuffle[k];
                listToShuffle[k] = listToShuffle[n];
                listToShuffle[n] = value;
            }

            return(listToShuffle);
        }
示例#3
0
 /// <summary>Given an Array of items, return a random item from the List.</summary>
 /// <typeparam name="T">The type of item in the given <paramref name="lst" />.</typeparam>
 /// <param name="lst">The list to get a random item from.</param>
 /// <returns>The random item as returned from <paramref name="lst" />.</returns>
 public static T GetRandomItem <T>(this Array lst)
 {
     return((T)lst.GetValue(NumberRandomizer.Next(0, lst.Length)));
 }
示例#4
0
 /// <summary>Given an ArrayList of items, return a random item from the List.</summary>
 /// <typeparam name="T">The type of item in the list <paramref name="lst" />.</typeparam>
 /// <param name="lst">The list to extract a random item from.</param>
 /// <returns>A random item of type <see cref="T" /> from the list.</returns>
 public static T GetRandomItem <T>(this ArrayList lst)
 {
     return((T)lst[NumberRandomizer.Next(0, lst.Count)]);
 }
示例#5
0
 /// <summary>Given an array of items, return a random item from the List.</summary>
 /// <typeparam name="T">The type of item in the array <paramref name="lst" />.</typeparam>
 /// <param name="lst">The list to extract a random item from.</param>
 /// <returns>The randomized item of type <see cref="T" /> to return from the array.</returns>
 public static T GetRandomItem <T>(this T[] lst)
 {
     return(lst[NumberRandomizer.Next(0, lst.Length)]);
 }
示例#6
0
 /// <summary>Given a HashSet of items, return a random item from the List.</summary>
 /// <param name="lst">The list to extract a random item from.</param>
 /// <typeparam name="T">The type of the item in the <paramref name="lst" />.</typeparam>
 /// <returns>The item of type <see cref="T" /> returned by the randomizer.</returns>
 public static T GetRandomItem <T>(this HashSet <T> lst)
 {
     return(lst.ToList()[NumberRandomizer.Next(0, lst.Count)]);
 }