分享一个随机重排函数(C#)

/// <summary>
        /// Get the no repeat random array
        /// </summary>
        /// <param name="minNumber">The min value of this random array</param>
        /// <param name="maxNumber">The max value of this random array</param>
        /// <param name="len">The result ranodm array's length</param>
        /// <returns>The result random array</returns>
        static int[] GetNoRepeatRandomArray(int minNumber, int maxNumber, int len)
        {
            int[] result = new int[len];
            Random ran = new Random();
            int[] index = new int[maxNumber];
            for (int i = 0; i < index.Length; i++)
            {
                index[i] = i;
            }
            int id;
            for (int i = 0; i < len; i++)
            {
                id = ran.Next(minNumber, maxNumber);
                result[i] = index[id];
                index[id] = index[maxNumber - 1];
                maxNumber--;
            }
            return result;
        }

 

posted @ 2013-12-17 14:08  zhangsir  阅读(227)  评论(0编辑  收藏  举报