Ejemplo n.º 1
0
        // TODO: Test
        /// <summary>
        ///     Shuffles the items in the list according to the specified random source.
        /// </summary>
        /// <param name="list">The list to shuffle.</param>
        /// <param name="random">The random source.</param>
        public static void Shuffle <T>(this SCG.IList <T> list, Random random)
        {
            #region Code Contracts

            // Argument must be non-null
            Requires(list != null, ArgumentMustBeNonNull);

            // List must be non-read-only
            Requires(!list.IsReadOnly, CollectionMustBeNonReadOnly);

            // The elements are the same
            Ensures(list.HasSameAs(OldValue(list.ToList())));

            #endregion

            if (random == null)
            {
                random = new Random(); // TODO: Use C5.Random?
            }

            var n = list.Count;
            while (--n > 0)
            {
                list.Swap(random.Next(n + 1), n);
            }
        }