打赏

扑克牌洗牌

class Program
    {
        static void Main(string[] args)
        {
            randomSortTest();

        }

        private static void randomSortTest()
        {
            int[] array = new int[54];
            for (int i = 0; i < 54; i++)
            {
                array[i] = i + 1;
            }
            var result = SortHelper.randomSort(array);
            for (int i = 0; i < result.Length; i++)
            {
                Console.WriteLine(result[i]);
            }
            Console.ReadKey();
        }
    }
    public class SortHelper
    {
        /// <summary>
        /// 扑克牌洗牌
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static int[] randomSort(int[] array)
        {
            Random random = new Random();
            int tmp = 0;
            for (int i = 0; i < array.Length-1; i++)
            {
                //产生[i+1,array.length)的值
                int j = random.Next(i + 1, array.Length);
                tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
            return array;
        }
    }

  参考;

https://www.cnblogs.com/haogj/archive/2011/04/08/2009519.html

posted @ 2018-08-20 09:05  刘奇云  阅读(142)  评论(0编辑  收藏  举报