当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

快速生成指定大小的随机不重复int数组的方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:48:36

:2011-02-09 14:17:53

一个用来快速生成指定大小的随机不重复int数组的实用方法

/// <summary>
/// 随机产生考场号
/// </summary>
/// <param name="start">初始值</param>
/// <param name="count">数量</param>
/// <returns></returns>
public static List<int> GetRandomList(int start, int count)
{
     List
<int> list = new List<int>();
     List
<int> temp = new List<int>();
     for (int i = start; i < count + start; i++)
    {
         temp.Add(i);
     }
     Random ro
= new Random();

     while (list.Count < count)
     {
         int a = ro.Next(0, temp.Count);
         if (!list.Contains(temp[a]))
         {
             list.Add(temp[a]);
             temp.Remove(temp[a]);
          }
     }
     return list;
}

原问地址:http://www.cnblogs.com/13142088/archive/2011/01/31/1948175.html



快速生成指定大小的随机不重复int数组的方法