Fisher-Yates 算法用于随机排列数组元素。

其做法很简单,先随机抽取一个,然后从剩余没被抽取的元素中继续抽取。在实现好的情况下,这个算法可以原地排序。

完。

哦,写点代码吧。

using System;  
using System.Collections.Generic;  
using System.Linq;  
using Random = UnityEngine.Random;  
  
namespace FishONU.Utils  
{  
    public static class Algorithm  
    {  
        public static void FisherYatesShuffle<T>(this IList<T> list)  
        {            for (int i = 0; i < list.Count; i++)  
            {                var j = Random.Range(0, i + 1);  
                (list[j], list[i]) = (list[i], list[j]);  
            }  
        }
    }  
}

完。