コード例 #1
0
        /// <summary>
        /// Shuffles the given list using the given <see cref="SecureRandom"/> generator.
        /// </summary>
        /// <typeparam name="T">The type of the members in <paramref name="this"/>.</typeparam>
        /// <param name="this">The list to shuffle.</param>
        /// <param name="random">The random number generator to use.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="this"/> or <paramref name="random"/> is <c>null</c>.</exception>
        /// <remarks>
        /// The implementation of <c>Shuffle</c> uses the Fisher¨CYates shuffle, as implemented by Durstenfeld -
        /// see http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle for details on this implementation.
        /// </remarks>
        public static void Shuffle <T>(this IList <T> @this, Random random)
        {
            @this.CheckParameterForNull("@this");
            random.CheckParameterForNull("random");

            var length = @this.Count;

            while (length > 1)
            {
                length--;
                var nextIndex = random.Next(length + 1);
                @this.Swap(nextIndex, length);
            }
        }