当前位置 > 首页 > Asp.net

How to pick numbers at random from a given set of numbers in c#

2009-10-1 20:33:00来源:Asp.net

The Random class defined in the .NET Framework provides functionality to generate pseudo-random number. According to MSDN, the current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random.

However, current implementation does not provide the following two functionalities.

  1. Picking random numbers from a given set of numbers: For example, you want random number from the given set of numbers. You have a set of numbers like (2, 4, 13, and 21).You would like to pick one number at a time at random from the list.
  2. Picking random numbers from a range of numbers with no repetition: Current implementation return random numbers within a range with some duplicate number. For example, you want random number from a range of numbers like (4 to 21). You would like to pick one number at a time at random from that range without any repetition or duplicate

However, these two requirements are easy to implement by using the Random class of the .NET Framework. I needed these two functionalities for one of my project, so I wrote a class to implement these two features. I think that this is a trivial work but I am posting it because it can save some of your time. To implement these two functionalities following class named RandomNumberFromAGivenSetOfNumbers is written.

   1: class RandomNumberFromAGivenSetOfNumbers
   2:    {
   3:        List<int> _setOfNumbers = new List<int>();
   4:  
   5:        public List<int> SetOfNumbers
   6:        {
   7:            get { return _setOfNumbers; }
   

  • DotNET企业架构应用实践-系列目录

    2010-10-27 11:41:00

    系列介绍 我一直在写关于AgileEAS.NET平台的一系列文章,也一直在推广AgileEAS.NET平台,本来也无意于独立的写这么一个系列,最早我是混杂在AgileEAS.NET平台中进行

  • 到底String对象有没有被CLR缓存?

    2009-3-29 19:17:00

    摘要: 刚看了一篇关于享元模式的文章(http://www.cnblogs.com/xinyuperfect/archive/2009/03/29/1424255.html)。里面提到了string

  • Reflector 5 Released

    2007-2-21 14:46:00

    Lutz released Reflector for .NET 5.0 If you do any .NET development and you dontuse Reflector f

  • Best Practice: GetProcessHeaps 和 GetProcessHeap 的使用。(The best practice of GetProcessHeaps and GetProcessHeap)

    2009-3-31 3:57:00

    GetProcessHeaps 返回当前进程的进程堆(process heap)的handle加上由HeapCreate创建的所有私有的heap的handle。 GetProcessHeap

  • Programming WCF Services翻译笔记(六)

    2007-9-26 22:42:00

    随着版本的不断演化,客户端与服务端的数据契约可能会出现版本不一致的情况。在WCF中,关于数据契约的版本控制有两种情况:新增成员与缺失成员。新增成员是指发送方包含了新增成员,默认处理方式为忽略新增成员

  • Fun with IronPython and Cecil (Part I)翻译

    2007-4-4 13:55:00

    什么是Cecil “Cecil是由Jb Evain 开发,用于生成和浏览ECMA CIL 格式的程序和函数库。它完全支持泛型,支持部分调试符号。简单说来,利用Cecil,你可以加载存在

  • SQL基础(二)

    2007-4-4 13:55:00

    SQL Server 数据库的高级操作 (1) 批处理 (2) 变量 (3) 逻辑控制 (4) 函数 (5) 高级查询 */ (1)批处理 将多条SQL语句作为一个整体去编译,生成一个执

  • 博客堂2007年度"不谈技术"年会

    2007-1-30 19:40:00

    今天上午参加了微软的Windows Vista大众市场发布会,不知道有多少位博客堂的老友们参加了此次会议。除了在嘉里饭店宴会厅的主会场以外,我们在各大卖场如中关村e世界以及国美电器等,还会持续举办V

  • Tool: Balsamiq Mockups Review

    2009-9-1 10:18:00

    One of the most recurrent activities in the Software Development Process, is create prototypes to r

  • Ext GWT 2.0 开发入门

    2010-1-8 15:38:00

    阅读: 710 评论: 4 作者: 一个人编程 发表于 2010-01-07 23:23 原文链接 一、准备: 1. 下载 Ext GWT 2.1.0 。 2. 下载 GWT 1.7