这种方法一般的用途就是将一批要显示的数据乱序随机显示,而我当初写这个方法也是抄袭的QQ密码提问的做法
原始的写法是这样的:
public static List<int> GetRandomList(this int maxValue)
{
Random rd = new Random();
List<int> list = new List<int>();
while (list.Count != maxValue)
{
int tempInt = rd.Next(maxValue);
if (!list.Contains(tempInt))
{
list.Add(tempInt);
}
}
return list;
}
这样的写法随机序列能够得出,但必定会存在一些重复无用功(生成的随机数重复出现,而且重复次数无法确定),而且已有的序列越多,重复的次数也就越多,极端情况下才会出现随机maxValue次,一般情况下都是maxValue + N次(N一般在maxValue以上)
最近手头无事,整理了下以前写的通用方法,然后将此方法优化了下,新方法如下:
/// <summary>
/// 生成最大值范围内无重复值的长度为最大值的随机序列,例:6,则返回0,1,2,3,4,5 的List
/// </summary>
/// <param name="maxValue"></param>
/// <returns></returns>
public static List<int> GetRandomList(this int maxValue)
{
if (maxValue == 0)
{
return null;
}
//逻辑描述:生成从0开始到maxValue的tempList
//然后random一次就maxValue--,并将random出来的整数用做索引,加入到returnList并从tempList中移除
maxValue = Math.Abs(maxValue);//防止负数
List<int> tempList = new List<int>();
for (int i = 0; i < maxValue; i++)
{
tempList.Add(i);
}
Random rd = new Random();
List<int> returnList = new List<int>();
while (maxValue > 0)
{
int tempInt = 0;
if (maxValue > 1)//当maxValue为1时,不再进行随机,因为还剩一个数字,无需随机
{
tempInt = rd.Next(maxValue);
}
returnList.Add(tempList[tempInt]);
tempList.RemoveAt(tempInt);
maxValue--;
}
return returnList;
}
此方法是先遍历出一个临时的List<int>存放所有小于maxValue的字段,然后以这个maxValue作为种子生成随机数,然后用该随机数作为索引从临时List中取出对应索引的整数放入returnList中,并将该整数从临时的List中移除
第二种方法的好处是不会存在重复的无用功,只需随机maxValue - 1次即可
自从有了Linq后,有了第三种方法……
Enumerable.Range(start, count).OrderBy(i => new Guid())