洗牌算法打乱数字排序


    /// <summary>
    /// 洗牌算法打乱数字排序
    /// </summary>
    /// <param name="arr"></param>
    /// <returns></returns>
    public static Texture[] FisherYatesshuffle(Texture[] arr)
    {
        // 打乱数组元素的索引
        System.Random rand = new System.Random();
        for (int i = arr.Length - 1; i > 0; i--)
        {
            int j = rand.Next(0, i + 1);
            Texture temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        // 输出打乱后的数组
        return arr;
    }

猜你喜欢

转载自blog.csdn.net/qq_38513810/article/details/137049353