相关疑难解决方法(0)

使用Random和OrderBy是一个很好的shuffle算法吗?

我在Coding Horror上读过一篇关于各种shuffle算法的文章.我已经看到人们已经在某个地方对列表进行了洗牌:

var r = new Random();
var shuffled = ordered.OrderBy(x => r.Next());
Run Code Online (Sandbox Code Playgroud)

这是一个很好的shuffle算法吗?它是如何工作的?这样做是否可以接受?

c# algorithm shuffle

160
推荐指数
5
解决办法
4万
查看次数

如何随机排序IEnumerable <>?

我有这个IEnumerable:

IEnumerable<MyObject>
Run Code Online (Sandbox Code Playgroud)

我需要随机订购MyObject列表.我需要强制转换为ArrayList吗?

或者我可以直接做到吗?谢谢

编辑

这是我的实际随机顺序函数:

IList<ArchiePacchettoOfferta> list = new List<ArchiePacchettoOfferta>(m_oEnum);
for (int i = 0; i < list.Count; ++i)
{
    HttpContext.Current.Response.Write(list[i].Titolo + "<br />");
}

Random rnd = new Random();
for (int i = 0; i < list.Count; ++i)
{
    int swapIndex = rnd.Next(i + 1);
    ArchiePacchettoOfferta tmp = list[i];
    list[i] = list[swapIndex];
    list[swapIndex] = tmp;
}

for (int i = 0; i < list.Count; ++i)
{
    HttpContext.Current.Response.Write(list[i].Titolo + "<br />");
}
Run Code Online (Sandbox Code Playgroud)

它每次以相同的方式命令列表:(

c# ienumerable

3
推荐指数
1
解决办法
3470
查看次数

标签 统计

c# ×2

algorithm ×1

ienumerable ×1

shuffle ×1