c#數組亂序,打亂數組


按照random隨機給出的index,進行兩兩交換,當然也存在與上一次一樣的數組結果。官方還有一種ICompare的比較器,只是打亂順序這個沒用起來,不知道該怎么搞,╮(╯_╰)╭

public static List<T> SortRandom<T>(this List<T> collection)
        {
            for (int i = collection.Count - 1; i > 0; i--)
            {
                Random rand = new Random();
                int p = rand.Next(i);
                var temp = collection[p];
                collection[p] = collection[i];
                collection[i] = temp;
            }
            return collection;
        }

 

事務處理,不過這個返回結果好像不怎么對

var arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
arr.Sort(delegate(int a, int b) { return (new Random()).Next(-1, 1); });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM