publicstaticvoidListRandom<T>(List<T> sources) { Random rd = new Random(); int index = 0; T temp; for (int i = 0; i < sources.Count; i++) { index = rd.Next(0, sources.Count - 1); if (index != i) { temp = sources[i]; sources[i] = sources[index]; sources[index] = temp; } } }
方法二
1 2 3 4 5 6 7 8 9 10
publicstaticList<T> ListRandom<T>(List<T> sources) { var random = new Random(); var resultList = new List<T>(); foreach (var item in sources) { resultList.Insert(random.Next(resultList.Count), item); } return resultList; }