C#.NET 随机排序集合(列表\数组) | 打乱集合(列表\数组)
直接上代码:
/// <summary>
/// 重排列表(打乱列表)
/// </summary>
/// <param name="arr"></param>
public static List<string> ConfusionArray(List<string> list)
{
Random random = new Random();
return list.OrderBy(x => random.Next()).ToList();
}
实际测试:
namespace Test
{
public class Program
{
public static void Main(string[] args)
{
List<string> list = new List<string>()
{
"1",
"2",
"3",
"4",
"5",
"6",
};
list = ConfusionArray(list);
foreach (var item in list)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 重排列表(打乱列表)
/// </summary>
/// <param name="arr"></param>
public static List<string> ConfusionArray(List<string> list)
{
Random random = new Random();
return list.OrderBy(x => random.Next()).ToList();
}
}
}
本文来自博客园,作者:重庆熊猫,转载请注明原文链接:https://www.cnblogs.com/cqpanda/p/17013538.html