如何在牛肉中洗净List
?我想在Random
上添加一个扩展,就地改写List
:
using System.Collections.Generic;
namespace System
{
extension Random
{
public virtual void Shuffle<T>(List<T> list)
{
// Use this to shuffle the list in-place
}
}
}
答案 0 :(得分:0)
使用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;
}
}
}
}