gpt4 book ai didi

c# - 在 C# 中随机重新排列项目列表的最佳方法是什么?

转载 作者:太空狗 更新时间:2023-10-29 20:47:14 25 4
gpt4 key购买 nike

我有一个对象列表,我想根据每个请求对它们随机重新排序。这样做的最佳方法是什么?

最佳答案

怎么样Knuth-Fisher-Yates shuffle algorithm

for (int i = cards.Length - 1; i > 0; i--)
{
int n = rand.Next(i + 1);
Swap(ref cards[i], ref cards[n]);
}

代码取自 Coding Horror .这也是一本关于人们经常犯错的推荐读物。

关于c# - 在 C# 中随机重新排列项目列表的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/417831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com