算法_洗牌算法shuffle Day0824

一个有序数组怎样改成无序的

public static void Shuffle(this IListlist)

{

                   Random rng = new Random();//定义一个随机范围

                       int n = list.Count;

                  while (n > 1)

                 {

                 //Random.Next(maxvalue)返回一个小于所指定最 大值的非负随机数

                  int k = rng.Next(n + 1);

                swap(list[n],list[k])

                      n--;

                 }

}

你可能感兴趣的:(算法_洗牌算法shuffle Day0824)