C# 生成指定范围内不重复的随机数 - 代码示例
{"title":"C# 生成指定范围内不重复的随机数 - 代码示例", "description":"本文提供C#代码示例,使用Random类和HashSet来生成指定范围内(例如1到10)的不重复随机数(例如5个)。", "keywords":"C#, 随机数, 不重复, 随机数生成, HashSet, Random类", "content":""using System;\nusing System.Collections.Generic;\n\npublic class Program\n{\n public static void Main(string[] args)\n {\n int min = 1;\n int max = 10;\n int count = 5;\n \n Random random = new Random();\n HashSetmin和最大值max,以及要生成的数字个数count。然后,使用Random类生成随机数,并将其添加到HashSet中。当HashSet中的元素个数达到指定的个数时,停止生成随机数。最后,遍历HashSet中的元素并输出。
原文地址: https://www.cveoy.top/t/topic/p1RH 著作权归作者所有。请勿转载和采集!