public class RandomEx
{
static Random r = new Random();
/// <summary>
/// 递归 生成不重复的随机数 ///不存在死循环
/// </summary>
/// <param name="start">随机数范围:开始</param>
/// <param name="end">随机数范围:结束</param>
/// <param name="len">随机数个数</param>
/// <param name="list">生成的随机数</param>
public static void Random(int start, int end, int len, List<int> list)
{
if (list.Count == len||start>end) return;
int i= r.Next(start, end);
if (!list.Contains(i))
list.Add(i);
if (i > start)
Random(start, i-1, len, list);
if (i < end)
Random(i+1, end, len, list);
}
}
C# 生成不重复的随机数
最新推荐文章于 2024-01-11 14:15:01 发布