List 如何洗牌列表?

List 如何洗牌列表?,list,random,beef,List,Random,Beef,如何在牛肉中洗牌列表?我想在Random中添加一个扩展,用于将列表混洗到位: using System.Collections.Generic; namespace System { extension Random { public virtual void Shuffle<T>(List<T> list) { // Use this to shuffle the list in-place

如何在牛肉中洗牌
列表
?我想在
Random
中添加一个扩展,用于将
列表
混洗到位:

using System.Collections.Generic;

namespace System
{
    extension Random 
    {
        public virtual void Shuffle<T>(List<T> list)
        {
            // Use this to shuffle the list in-place
        }
    }
}
使用System.Collections.Generic;
名称空间系统
{
扩展随机
{
公共虚拟无效洗牌(列表)
{
//使用此选项将列表洗牌到位
}
}
}

使用Fisher-Yates洗牌算法:

using System.Collections.Generic;

namespace System
{
    extension Random 
    {
        public virtual void Shuffle<T>(List<T> list)
        {
            for (let i = list.Count - 1; i > 0; i--) 
            {
                let j = Next(0, i + 1);
                let tmp = list[j];
                list[j] = list[i];
                list[i] = tmp;
            }
        }
    }
}
使用System.Collections.Generic;
名称空间系统
{
扩展随机
{
公共虚拟无效洗牌(列表)
{
对于(让i=list.Count-1;i>0;i--)
{
设j=Next(0,i+1);
设tmp=list[j];
列表[j]=列表[i];
列表[i]=tmp;
}
}
}
}