关于dalmeeme的洗牌算法

636f6c696e 2012-02-23 03:31:33
 int[] numbers = new int[100];
for (int i = 0; i < 100; i++)
numbers[i] = i;
Random rd = new Random();
int index, realLength = numbers.Length, temp;
for (int i = 0; i < 100; i++)
{
index = rd.Next(realLength);
Response.Write(numbers[index] + "<br/>");
temp = numbers[index];
numbers[index] = numbers[realLength - 1];
numbers[realLength - 1] = temp;
realLength--;
}



经过网友Arcan的提醒,我看了下他的算法实质是 for i:=1 to n do swap(a[i], a[random(i,n)]);
而并非for i:=1 to n do swap(a[i], a[random(1,n)]),只不过是倒着换的。
我没有看清楚算法就误认为他的解法有误,在此对他表示抱歉。
...全文
173 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyonghui123 2012-08-10
  • 打赏
  • 举报
回复
Random ram = new Random();
int currentIndex;
Product tempValue;
for (int i = 0; i < listtemp.Count; i++)
{
currentIndex = ram.Next(0, listtemp.Count - i);
tempValue = listtemp[currentIndex];
listtemp[currentIndex] = listtemp[listtemp.Count - 1 - i];
listtemp[listtemp.Count - 1 - i] = tempValue;
}
liyonghui123 2012-08-10
  • 打赏
  • 举报
回复
Random ram = new Random();
int currentIndex;
Product tempValue;
for (int i = 0; i < listtemp.Count; i++)
{
currentIndex = ram.Next(0, listtemp.Count - i);
tempValue = listtemp[currentIndex];
listtemp[currentIndex] = listtemp[listtemp.Count - 1 - i];
listtemp[listtemp.Count - 1 - i] = tempValue;
}
dalmeeme 2012-02-23
  • 打赏
  • 举报
回复
我接受楼主的道歉。这次冲突我也有过错,在这里也为向楼主道歉。
nonocast 2012-02-23
  • 打赏
  • 举报
回复
没事,他会理解的
misery2011 2012-02-23
  • 打赏
  • 举报
回复
嗯,是这样的。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧