对数组随机排序

作者:追风剑情 发布于:2016-1-13 20:15 分类:Algorithms

public static void RandSortArray(List<int> arr)
{
        int size = arr.Count;
        int seed = 10000;
        //对给定数组随机排序, 每组排列的概率都为 1/size的阶乘
        System.Random rand = new System.Random();
        for (int i = 0; i < size; i++)
        {
            int randIndex = (int)(rand.Next(seed) % (size - i)) + i;//产生一个[i, size-1]的随机数
            int j = arr[i];
            arr[i] = arr[randIndex];
            arr[randIndex] = j;
        }
}

标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号