早上想到另一个方法,可能更合适,昨天的方法会有边界问题,比如2后面一定是跟1
新的方法如下

int[] NumList=new int[20]...{1,2,3,4,5,6,7,8,9,10,.....20};
private Random random=new Random;
public void GetRandom(ref int[] numList)
...{
int index;
int chang;
for(int iCount=19;iCount>0;iCount--)
...{
index=random.Next(0,iCount);
chang=numList[index];
numList[index]=numList[iCount];
numList[iCount]=chang;
}
}相当与对数组做随机交换,时间长度为0(n)

介绍了一种有效的时间复杂度为O(n)的数组随机排列方法,通过随机交换元素位置实现,解决了原有方法存在的边界问题。
16万+

被折叠的 条评论
为什么被折叠?



