博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
向长度为100的数组里插入随机数不能重复
阅读量:4591 次
发布时间:2019-06-09

本文共 1110 字,大约阅读时间需要 3 分钟。

方法一:
int[] intArr = new int[100]; ArrayList myList = new ArrayList(); Random rnd = new Random(); while (myList.Count < 100) { int  = rnd.Next(1, 101); if (!myList.Contains()) myList.Add(); } for (int i = 0; i <100; i++) { intArr[i] = (int)myList[i]; Console.Write("{0} ", intArr[i]); Console.WriteLine(); }
方法二: public int[] GetNoRepeatArrayy(int arrLength) 2         { 3             int[] array = new int[arrLength]; 4             IList
list = new List
(); 5 //准备不重复的数据 6 for (int i = 0; i < array.Length; i++) 7 { 8 list.Add(i); 9 }10 //将不重复的数据随机插入到数组中11 for (int j = (list.Count - 1); j > -1; j--)12 {13 //获得数据的随机索引14 int index = new Random(Guid.NewGuid().GetHashCode()).Next(0, list.Count);15 //给数组赋值16 array[j] = list[index];17 //删除废弃数据,避免重复数据插入到数组中18 list.RemoveAt(index);19 }20 return array;21 }

转载于:https://www.cnblogs.com/dulang/p/6699043.html

你可能感兴趣的文章
win8自带输入法如何切换全角、半角操作流程
查看>>
TensorFlow windows 安装(base anaconda)
查看>>
Percona XtraDB Cluster集群
查看>>
mybatis学习笔记1--HelloMybatis
查看>>
正则表达式全局匹配网址
查看>>
js多张图片合成一张图,canvas(海报图,将二维码和背景图合并) -----vue
查看>>
前端页面刷新与跳转
查看>>
笔记本链接公司内网(跨网段) ,然后保证wifi
查看>>
Httpd做应用代理
查看>>
如何用Jmter生成合法的手机号
查看>>
Jmeter生成正常的人名
查看>>
Jmeter 做压力测试步骤
查看>>
jmeter生成随机的四位数
查看>>
Jmeter做接口的压力测试
查看>>
sql语句优化的30种方法
查看>>
MyISAM和InnoDB的区别
查看>>
springboot2.0 management.security.enabled无效
查看>>
spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings
查看>>
源发行版8需要目标发行版1.8
查看>>
Cleartext HTTP traffic to xxx not permitted解决办法
查看>>