重庆熊猫 Loading

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();
        }
    }
}

posted @ 2023-01-01 21:53  重庆熊猫  阅读(1379)  评论(0编辑  收藏  举报