
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
| int[] index = new int[15]; for (int i = 0; i < 15; i++) index = i; Random r = new Random(); //用来保存随机生成的不重复的10个数 int[] result = new int[10]; int site = 15;//设置上限 int ID; for (int j = 0; j < 10; j++) { ID = r.Next(1,site - 1); //在随机位置取出一个数,保存到结果数组 result[j] = index[ID]; //最后一个数复制到当前位置 index[ID] = index[site - 1]; //位置的上限减少一 site--; } |
| Hashtable hashtable = new Hashtable(); Random rm = new Random(); int RmNum = 10; for (int i = 0; hashtable.Count < RmNum; i++) { int nValue = rm.Next(100); if (!hashtable.ContainsValue(nValue) && nValue != 0) { hashtable.Add(nValue,nValue); Console.Writeline(nValue.ToString()); } } |
| Random ra=new Random(unchecked((int)DateTime.Now.Ticks)); int[] arrNum=new int[10]; int tmp=0; int minValue=1; int maxValue=10; for (int i=0;i<10;i++) { tmp=ra.Next(minValue,maxValue); //随机取数 arrNum=getNum(arrNum,tmp,minValue,maxValue,ra); //取出值赋到数组中 } ......... ......... public int getNum(int[] arrNum,int tmp,int minValue,int maxValue,Random ra) { int n=0; while (n<=arrNum.Length-1) { if (arrNum[n]==tmp) //利用循环判断是否有重复 { tmp=ra.Next(minValue,maxValue); //重新随机获取。 getNum(arrNum,ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。 } n++; } return tmp; } |
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的C# Random 生成不重复随机数全部内容,希望文章能够帮你解决C# Random 生成不重复随机数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)