生成最大值范围内无重复值的长度为最大值的随机序列List<int>
来源:互联网 发布:化工仿真软件 编辑:程序博客网 时间:2024/06/04 19:46
这种方法一般的用途就是将一批要显示的数据乱序随机显示,而我当初写这个方法也是抄袭的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())
- 生成最大值范围内无重复值的长度为最大值的随机序列List<int>
- 求无符号int 的最大值
- 生成指定范围内无重复的随机乱序数组(JAVA实现/时间复杂度为O(n))
- js生成指定范围内指定长度随机不重复的字符串数组
- int的最大值
- 求int 的最大值
- 关于int的最大值
- 在0~N(不包括N)范围内随机生成一个长度为M(M <= N)且内容不重复的数组
- 查找序列的最大值
- 求int型List集合的最小值最大值排序
- 随机生成给定范围内N个不重复的数
- ab的最大值为
- php int型的最大值
- int最大值,最小值的获取
- 在较大范围内生成大量的无重复的随机数
- C++中生成int类型的最大值和最小值方法
- java 无重复生成一个范围内的随机数
- Python生成一定范围内指定数目的无重复随机数
- make的自动变量
- 自动添加修改环境变量
- Android 播放SD卡上指定的一首歌(初级版)
- Drawable
- 判断文件是否存在,判断是否为文件夹
- 生成最大值范围内无重复值的长度为最大值的随机序列List<int>
- JS 跳转到指定Action
- git使用总结
- Windows系统编程之异步I/O和完成端口
- 斩断DDoS魔掌的六把利剑(四)
- 一次Sqlalchemy从model类定义到数据库表的SQL脚本自动生成日志
- JDK 1.7的新特性
- Qt学习之路(19): 事件(event)
- js控制导航变色 特效