using System;
using System.Collections.Generic;
using System.Text;
public class MyCollections
{
public static void shuffle<T>(ref List<T> list)
{
Random rand = new Random(Guid.NewGuid().GetHashCode());
List<T> newList = new List<T>();//儲存結果的集合
foreach (T item in list)
{
newList.Insert(rand.Next(0, newList.Count), item);
}
newList.Remove(list[0]);//移除list[0]的值
newList.Insert(rand.Next(0, newList.Count), list[0]);//再重新隨機插入第一筆
list = newList;
}
}
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleShuffle
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
//Step 1.加入字串Apple、Banana、Blueberry、Cherry、Grape
list.Add("Apple"); list.Add("Banana"); list.Add("Blueberry");
list.Add("Cherry"); list.Add("Grape");
//Step 2.打亂順序
MyCollections.shuffle(ref list);
//Step 3 .Net也可以很樂勝地顯示結果
foreach (string item in list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
http://www.dotblogs.com.tw/shadow/archive/2011/12/30/63779.aspx