C# 实现M-N之间随机取K个不重复的数
程序员文章站
2024-03-15 18:54:36
...
public static int[] arrayRandom(int min,int max, int count) {
//生成有序数list
List<int> originList = new List<int>();
int listcount = max - min + 1;
for (int i = 0; i < listcount; i++)
{
originList.Add(i + min);
}
//生成随机数list
List<int> callback = new List<int>();
Random random = new Random();
for (int i = 0; i < count; i++)
{
int place = random.Next(0, listcount - i);
callback.Add(originList[place]);
originList.Remove(originList[place]);
}
return callback.ToArray();
}
下一篇: 求1到n的阶乘之和