511遇见论坛

 找回密码
 立即注册
查看: 1765|回复: 0

c sharp调用Random类生成不重复的随机数的方法

[复制链接]
发表于 2019-11-25 09:35:30 | 显示全部楼层 |阅读模式
当Random 类无参数时,会自动调用系统时钟作为随机种子值,下面我们通过几种方法来实现C#生成不重复的随机数。
方法1:数组来保存索引号,先随机生成一个数组位置
  1. //产生不重复的随机数
  2. namespace ArrayRandTwo
  3. {
  4.     class Program
  5.     {      
  6.         static void Main(string[] args)
  7.         {
  8.             int[] a = new int[15];
  9.             for (int i = 0; i < a.Length ; i++)
  10.                 a[i] = i;
  11.             Random r = new Random();
  12.             //新的数组result用来保存随机生成的不重复的10个数
  13.             int[] result = new int[10];
  14.             //设置上限
  15.             int ulimit = 15;
  16.             int id;
  17.             for (int j = 0; j < 10; j++)
  18.             {
  19.                 id = r.Next(1, ulimit - 1);
  20.                 //在随机位置取出一个数,保存到结果数组
  21.                 result[j] = a[id];
  22.                 //最后一个数复制到当前位置
  23.                 a[id] = a[ulimit - 1];
  24.                 //位置的上限减少一
  25.                 ulimit--;
  26.             }
  27.             foreach (int k in result)
  28.             {
  29.                 Console.Write("{0} ", k);
  30.             }
  31.             Console.ReadKey();
  32.         }
  33.     }
  34. }
复制代码
方法2: 利用Hashtable
必须引进空间名:using System.Collections; Hashtable中文称作哈希表,也叫散列表,是根据key和value进行访问存储的数据结构
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;

  7. //Hashtable的命名空间System.Collections,通过using进行引入。
  8. namespace ArrayRandThree
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //实例化Hashtable
  15.             Hashtable hashtable = new Hashtable();
  16.             Random rm = new Random();
  17.             int RmNum = 100;
  18.             for (int i = 0; hashtable.Count < RmNum; i++)
  19.             {
  20.                 //产生随机数给nValue
  21.                 int nValue = rm.Next(100);
  22.                 if (!hashtable.ContainsValue(nValue) && nValue != 0)
  23.                 {
  24.                     ////增加元素nValue
  25.                     hashtable.Add(nValue, nValue);
  26.                     //value的遍历
  27.                     foreach (int value in hashtable.Values)
  28.                     {
  29.                         Console.WriteLine(value);
  30.                     }                              
  31.                 }
  32.                 Console.ReadKey();
  33.             }
  34.         }
  35.     }
  36. }
复制代码
方法3:List 类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace ArrayRandFour
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<int> listNum = new List<int>();
  13.             Random random = new Random();
  14.             //最小随机数
  15.             const int Min = 100;
  16.             //最小随机数
  17.             const int Max = 999;
  18.             //产生多少个随机数,这里是10个
  19.             const int Count = 10;
  20.             for (int i = 0; i < 100; i++)
  21.             {
  22.                 var num = random.Next(Min, Max);
  23.                 if (!listNum.Contains(num))
  24.                 {
  25.                     //将产生的随机数num添加到listNum尾部
  26.                     listNum.Add(num);
  27.                     //判断 达到设定的Count,跳出循环
  28.                     if (listNum.Count == Count)
  29.                     {
  30.                         break;
  31.                     }   
  32.                 }
  33.             }
  34.             //遍历 listNum
  35.             foreach (int k in listNum)
  36.             {
  37.                 Console.Write("{0} ", k);
  38.             }
  39.             Console.ReadKey();
  40.         }
  41.     }
  42. }
复制代码



511遇见论坛
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|511遇见论坛 ( 鲁ICP备15039387号-1 )|网站地图

GMT+8, 2024-4-19 21:52

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表