コード例 #1
0
        /// <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);
        }
コード例 #2
0
 /// <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())));
 }