[C#.NET][LINQ] 利用 Guid + LINQ 實作亂數排序且不重複
以取往亂數不重複相當的麻煩
http://www.dotblogs.com.tw/yc421206/archive/2008/12/20/6471.aspx
演化到現在的 .NET Framework 要實作就很簡單了
透過下列程式碼便可輕鬆取出不重複的號碼,在此就不再多做描述
//Random var random = new Random(); var sequence = Enumerable.Range(1,100).OrderBy (e => random.Next()); sequence.Dump(); } { //Random+Guid+HashCode var random = new Random(Guid.NewGuid().GetHashCode()); var sequence = Enumerable.Range(1,100).OrderBy (e => random.Next()); sequence.Dump(); } { //Guid var sequence = Enumerable.Range(1,100).OrderBy (e => Guid.NewGuid()); sequence.Dump(); }
執行的結果看起來夠亂,非常好!!!
文章出自:http://www.dotblogs.com.tw/yc421206/archive/2014/07/15/145964.aspx
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET