获取范围内指定个数不重复随机数

/// <summary>
    /// 获取范围内指定个数不重复随机数
    /// </summary>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <param name="count"></param>
    /// <returns></returns>
    public static int[] GetRandomNumbersArray(int min, int max, int count)
    {
        if (count > (max - min + 1) || max < min)
        {
            return null;
        }

        int[] result = new int[count];
        HashSet<int> set = new HashSet<int>(); //HashSet 不允许重复对象
        while (set.Count < count)
        {
            set.Add(UnityEngine.Random.Range(min, max + 1));
        }

        set.CopyTo(result);
        return result;
    }
上一篇:窗口无响应,关闭失败,报video system not initialized


下一篇:代码随想录:哈希表