Most efficient way to randomly “sort” (Shuffle) a list of integers in C#

前端 未结 12 1356
长发绾君心
长发绾君心 2020-11-22 12:25

I need to randomly \'sort\' a list of integers (0-1999) in the most efficient way possible. Any ideas?

Currently, I am doing something like this:

bo         


        
12条回答
  •  借酒劲吻你
    2020-11-22 13:08

    what about :

    System.Array.Sort(arrayinstance, RandomizerMethod);
    ...
    //any evoluated random class could do it !
    private static readonly System.Random Randomizer = new System.Random();
    
    private static int RandomizerMethod(T x, T y)
        where T : IComparable
    {
        if (x.CompareTo(y) == 0)
            return 0;
    
        return Randomizer.Next().CompareTo(Randomizer.Next());
    }
    

    voila!

提交回复
热议问题