Beispiel #1
0
        public static T[,] Random <T>(this T[,] array)
        {
            if (array == null)
            {
                throw CommonExceptions.ArgumentNull("array");
            }
            Contract.Ensures(Contract.Result <T[, ]>() != null);
            int w   = array.GetLength(1);
            int idx = array.Length;

            for (int i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (int j = w - 1; j >= 0; j--)
                {
                    int r = RandomExt.Next(idx--);
                    int x = r % w;
                    int y = r / w;
                    if (y != i || x != j)
                    {
                        T temp = array[i, j];
                        array[i, j] = array[y, x];
                        array[y, x] = temp;
                    }
                }
            }
            return(array);
        }
Beispiel #2
0
        public static T[, ,] Random <T>(this T[, ,] array)
        {
            int h   = array.GetLength(1);
            int w   = array.GetLength(2);
            int idx = array.Length;

            for (int i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (int j = h - 1; j >= 0; j--)
                {
                    for (int k = w - 1; k >= 0; k--)
                    {
                        int r = RandomExt.Next(idx--);
                        int x = r % w;
                        int z = r / w;
                        int y = z % h;
                        z /= h;
                        if (z != i || y != j || x != k)
                        {
                            T temp = array[i, j, k];
                            array[i, j, k] = array[z, y, x];
                            array[z, y, x] = temp;
                        }
                    }
                }
            }
            return(array);
        }
Beispiel #3
0
        public static T[,] Random <T>(this T[,] array)
        {
            CommonExceptions.CheckArgumentNull(array, nameof(array));
            Contract.Ensures(Contract.Result <T[, ]>() != null);
            var w   = array.GetLength(1);
            var idx = array.Length;

            for (var i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (var j = w - 1; j >= 0; j--)
                {
                    Contract.Assume(idx >= 0);
                    var r = RandomExt.Next(idx--);
                    var y = r / w;
                    var x = r - y * w;                     // r % w
                    if (y != i || x != j)
                    {
                        var temp = array[i, j];
                        array[i, j] = array[y, x];
                        array[y, x] = temp;
                    }
                }
            }
            return(array);
        }
Beispiel #4
0
        public static T[, ,] Random <T>(this T[, ,] array)
        {
            CommonExceptions.CheckArgumentNull(array, nameof(array));
            Contract.Ensures(Contract.Result <T[, , ]>() != null);
            var h   = array.GetLength(1);
            var w   = array.GetLength(2);
            var idx = array.Length;

            for (var i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (var j = h - 1; j >= 0; j--)
                {
                    for (var k = w - 1; k >= 0; k--)
                    {
                        Contract.Assume(idx >= 0);
                        var r = RandomExt.Next(idx--);
                        var t = r / w;
                        var x = r - t * w;                         // r % w
                        var z = t / h;
                        var y = t - z * h;                         // t % h
                        if (z != i || y != j || x != k)
                        {
                            var temp = array[i, j, k];
                            array[i, j, k] = array[z, y, x];
                            array[z, y, x] = temp;
                        }
                    }
                }
            }
            return(array);
        }
Beispiel #5
0
        public static T[, ,] Random <T>(this T[, ,] array)
        {
            if (array == null)
            {
                throw ExceptionHelper.ArgumentNull("array");
            }
            Contract.Ensures(Contract.Result <T[, , ]>() != null);
            int h   = array.GetLength(1);
            int w   = array.GetLength(2);
            int idx = array.Length;

            for (int i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (int j = h - 1; j >= 0; j--)
                {
                    for (int k = w - 1; k >= 0; k--)
                    {
                        int r = RandomExt.Next(idx--);
                        int x = r % w;
                        int z = r / w;
                        int y = z % h;
                        z /= h;
                        if (z != i || y != j || x != k)
                        {
                            T temp = array[i, j, k];
                            array[i, j, k] = array[z, y, x];
                            array[z, y, x] = temp;
                        }
                    }
                }
            }
            return(array);
        }
Beispiel #6
0
        public static T[, ,] Random <T>(this T[, ,] array)
        {
            CommonExceptions.CheckArgumentNull(array, "array");
            Contract.Ensures(Contract.Result <T[, , ]>() != null);
            int h   = array.GetLength(1);
            int w   = array.GetLength(2);
            int idx = array.Length;

            for (int i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (int j = h - 1; j >= 0; j--)
                {
                    for (int k = w - 1; k >= 0; k--)
                    {
                        Contract.Assume(idx >= 0);
                        int r = RandomExt.Next(idx--);
                        int t = r / w;
                        int x = r - t * w;                         // r % w
                        int z = t / h;
                        int y = t - z * h;                         // t % h
                        if (z != i || y != j || x != k)
                        {
                            T temp = array[i, j, k];
                            array[i, j, k] = array[z, y, x];
                            array[z, y, x] = temp;
                        }
                    }
                }
            }
            return(array);
        }
Beispiel #7
0
 /// <summary>
 /// 将数组进行随机排序。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">要随机排序的数组。</param>
 /// <returns>要随机排序数组。</returns>
 /// <remarks>采用下面的代码进行测试:
 /// <code>int size = 10;
 /// int[] arr = new int[size];
 /// int[,] cnt = new int[size, size];
 /// for (int i = 0; i &lt; 200; i++)
 /// {
 ///     arr.Fill(n => n).Random();
 ///     for (int j = 0; j &lt; size; j++) cnt[j, arr[j]]++;
 /// }
 /// for (int i = 0; i &lt; size; i++)
 /// {
 ///     for (int j = 0; j &lt; size; j++)
 ///         Console.Write("{0} ", cnt[i, j]);
 ///     Console.WriteLine();
 /// }</code>
 /// </remarks>
 public static T[] Random <T>(this T[] array)
 {
     for (int i = array.Length - 1; i > 0; i--)
     {
         int j = RandomExt.Next(i + 1);
         if (j != i)
         {
             T temp = array[i];
             array[i] = array[j];
             array[j] = temp;
         }
     }
     return(array);
 }
Beispiel #8
0
 /// <summary>
 /// 将数组进行随机排序。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">要进行随机排序的数组。</param>
 /// <returns>已完成随机排序的数组。</returns>
 /// <remarks>应保证每个元素出现在每个位置的概率基本相同。
 /// 采用下面的代码进行测试:
 /// <code>int size = 10;
 /// int[] arr = new int[size];
 /// int[,] cnt = new int[size, size];
 /// for (int i = 0; i { 200; i++)
 /// {
 ///     arr.Fill(n => n).Random();
 ///     for (int j = 0; j { size; j++) cnt[j, arr[j]]++;
 /// }
 /// for (int i = 0; i { size; i++)
 /// {
 ///     for (int j = 0; j { size; j++)
 ///         Console.Write("{0} ", cnt[i, j]);
 ///     Console.WriteLine();
 /// }</code>
 /// </remarks>
 /// <overloads>
 /// <summary>
 /// 将数组进行随机排序。
 /// </summary>
 /// </overloads>
 public static T[] Random <T>(this T[] array)
 {
     CommonExceptions.CheckArgumentNull(array, nameof(array));
     Contract.Ensures(Contract.Result <T[]>() != null);
     for (var i = array.Length - 1; i > 0; i--)
     {
         var j = RandomExt.Next(i + 1);
         if (j != i)
         {
             var temp = array[i];
             array[i] = array[j];
             array[j] = temp;
         }
     }
     return(array);
 }
Beispiel #9
0
 /// <summary>
 /// 将数组进行随机排序。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">要进行随机排序的数组。</param>
 /// <returns>已完成随机排序的数组。</returns>
 /// <remarks>应保证每个元素出现在每个位置的概率基本相同。
 /// 采用下面的代码进行测试:
 /// <code>int size = 10;
 /// int[] arr = new int[size];
 /// int[,] cnt = new int[size, size];
 /// for (int i = 0; i { 200; i++)
 /// {
 ///     arr.Fill(n => n).Random();
 ///     for (int j = 0; j { size; j++) cnt[j, arr[j]]++;
 /// }
 /// for (int i = 0; i { size; i++)
 /// {
 ///     for (int j = 0; j { size; j++)
 ///         Console.Write("{0} ", cnt[i, j]);
 ///     Console.WriteLine();
 /// }</code>
 /// </remarks>
 /// <overloads>
 /// <summary>
 /// 将数组进行随机排序。
 /// </summary>
 /// </overloads>
 public static T[] Random <T>(this T[] array)
 {
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     Contract.Ensures(Contract.Result <T[]>() != null);
     for (int i = array.Length - 1; i > 0; i--)
     {
         int j = RandomExt.Next(i + 1);
         if (j != i)
         {
             T temp = array[i];
             array[i] = array[j];
             array[j] = temp;
         }
     }
     return(array);
 }
Beispiel #10
0
        public static T[,] Random <T>(this T[,] array)
        {
            int w   = array.GetLength(1);
            int idx = array.Length;

            for (int i = array.GetLength(0) - 1; i >= 0; i--)
            {
                for (int j = w - 1; j >= 0; j--)
                {
                    int r = RandomExt.Next(idx--);
                    int x = r % w;
                    int y = r / w;
                    if (y != i || x != j)
                    {
                        T temp = array[i, j];
                        array[i, j] = array[y, x];
                        array[y, x] = temp;
                    }
                }
            }
            return(array);
        }