.net中如何生成不重复的随机数
生活随笔
收集整理的这篇文章主要介绍了
.net中如何生成不重复的随机数
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
2019独角兽企业重金招聘Python工程师标准>>>
.net中生成不重复的随机数的方法
//获取count个不大于maxNumber的整数,所有整数不重复。当然,count必须小于等于maxNumberstatic List<int> GetRandomArray(int maxNumber,int count){List<int> list = new List<int>();//保存取出的随机数int[] array=new int[maxNumber];//定义初始数组for (int i = 0; i < maxNumber; i++)//给数组元素赋值array[i] = i + 1;Random rnd = new Random();for (int j = 0; j < count; j++){int index = rnd.Next(j,maxNumber);//生成一个随机数,作为数组下标int temp = array[index];//从数组中取出index为下标的数list.Add(temp);//将取出的数添加到list中array[index] = array[j];//将下标为j的数交换到index位置array[j] = temp;//将取出的数交换到j的位置}return list;}
参考资料:.net中生成不重复的随机数 http://www.studyofnet.com/news/977.html
转载于:https://my.oschina.net/u/2428791/blog/505742
总结
以上是生活随笔为你收集整理的.net中如何生成不重复的随机数的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: yii2-按需加载并管理CSS样式/JS
- 下一篇: 采用dlopen、dlsym、dlclo