C#数组随机排序

来源:互联网 发布:sql去重复行 编辑:程序博客网 时间:2024/05/17 06:31

public static T[] RandomSort<T>(T[] array)
        {
            int len = array.Length;

            Random rand = new Random();
            System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
            T[] ret = new T[len];
            int i = 0;
            while (list.Count < len)
            {
                int iter = rand.Next(0, len);
                if (!list.Contains(iter))
                {
                    list.Add(iter);
                    ret[i] = array[iter];
                    i++;
                }
            }
            return ret;
        }

原创粉丝点击