欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();
}
相关标签: 算法 c#