集合随机打乱顺序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random ran = new Random();
            List<model> newList = new List<model>();
            newList.Add(new model { name="A"});
            newList.Add(new model { name = "B" });
            newList.Add(new model { name = "C" });
            newList.Add(new model { name = "D" });
            int index = 0;
            var temp =new model();
            for (int i = 0; i < newList.Count; i++)
            {

                index = ran.Next(0, newList.Count - 1);
                if (index != i)
                {
                    temp = newList[i];
                    newList[i] = newList[index];
                    newList[index] = temp;
                }
            }
            foreach (var item in newList)
            {
                Console.Write(item.name);
            }
            Console.ReadLine(); 
           
        }
    }
    public class model
    {
        public string name { get; set; }
    }
}

  

原文地址:https://www.cnblogs.com/dandanwozhishidan/p/6607055.html