C#.NET产生在两个值之间的不重复随机数组

/// <summary>
        /// 产生在两个值之间的不重复随机数组,长度为aMax - aMin + 1
        /// </summary>
        /// <param name="aMin">最小值</param>
        /// <param name="aMax">最大值</param>
        /// <returns>随机数组</returns>
        static int[] RandomArray( int aMin, int aMax ) {
            int length = aMax - aMin + 1;
            int[] result = new int[length];
            List<int> list = new List<int>( length );
            for (int i = aMin; i <=aMax; i++) {
                list.Add( i );
            }
            Random rnd=new Random();
            for (int i = 0; i < length; i++) {
                int index=rnd.Next(0,list.Count-1);
                result[i] = list[index];
                list.RemoveAt( index );
            }
            return result;
        }
作者:一修先生
         
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/1971ruru/p/1709502.html