惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
H
Hacker News: Front Page
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tailwind CSS Blog
S
Secure Thoughts
N
Netflix TechBlog - Medium
T
The Exploit Database - CXSecurity.com
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
L
LangChain Blog
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
有赞技术团队
有赞技术团队
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
G
Google Developers Blog
T
Threatpost
小众软件
小众软件
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News

博客园 - zqf620

.Net PetShop 3.0中购物车总价计算的bug .NET PetShop 3.0 FAQ novalidate选项无效的问题 .NET方向高级开发人员面试时应该事先考虑的问题 (zt) UML中的图 从一个表中随即抽取100条记录 PL/SQL User's Guide and Reference, Release 2 (9.2) chm版 下载 Oracle中实现自动增长列 在开发过程中运用UML 输出到html页面的字符串的格式化 在DataGrid控件中编辑数据项 在DataGrid控件中获取数据项中各列的数据内容 DataGrid控件的分页 DTD简介 W3C XML Schema (XSD) XML相关技术概览 将web窗体页文件(test.aspx)转换成用户控件文件(test.ascx) access作为后台数据库遇到的访问权限问题 HTML实体
从一组数中每次抽取出一个数,并规定了每个数出现的概率
zqf620 · 2007-02-04 · via 博客园 - zqf620

从一组数中每次抽取出一个数,并规定了每个数出现的概率
这个问题,我是这样考虑的:
     假设有4个数字a、b、c,他们出现的概率分别为 10%,30%和60%。现在我随机生成一个0-99的随机数,如果随机数在0-9之间,则抽取a,随机数在10-39之间,则抽取b,随机数在40-99之间则抽取c。

实现代码如下:

using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;

namespace zqf620.Demo.WeightingNumber
{
 /// <summary>
 /// WeightingNumber 的摘要说明。
 /// </summary>
 public class WeightingNumber
 {
  public WeightingNumber()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  public WeightingNumber(params uint[] weightings) //构造函数
  {
   this.setWeighting(weightings);
  }

  private ArrayList _weightings = new ArrayList();
  private int _count = 0;

  public bool setWeighting(params uint[] weightings) //设置一组权重,要求所有权重之和为100
  {
   uint count = 0;
   uint min,max;
   foreach(uint i in weightings)
   {
    min = count*100;
    max = min + i*100 -1;
    zone tmp = new zone(min,max);
    _weightings.Add(tmp);
    count += i;
   }

   if (count != 100)
    return false;
   else
   {
    _count = _weightings.Count;
    return true;
   }
  }
  
  public int GetIndex()  //返回一组数的一个下标
  {
   int index = -1;

   if(_count == 0)
    return index;

   Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
   int num = rdm.Next(0,9999);
//   Debug.Write(num);   

            for(int i = 0; i < this._count; i++)
   {
    zone tmp = (zone)_weightings[i];
    if(num >= tmp.minValue && num <= tmp.maxValue)
    {
     index = i;
     return index;
    }
   }
   return index;
  }

  struct zone  //用于保存一个minValue到maxValue区域
  {
   public uint minValue,maxValue;
   
   public zone(uint min,uint max)
   {
    minValue = min;
    maxValue = max;
   }
  }

  
  static void Main()
  {
   int[] chars = new int[]{0,1,2,3,4};   //一组数,0 1 2 3 4
   //0出现的概率为10%,1出现的概率为20%,2出现的概率为40%,…… 这些概率值加起来必须为100
   WeightingNumber wn = new WeightingNumber(10,20,40,15,15); 

   ArrayList result = new ArrayList(200);

   for(int i = 0; i < 200 ;i++) //抽取200次
   {
    Thread.Sleep(1);
    int index = wn.GetIndex(); //抽取下标
    result.Add(chars[index]);  //得到下标对应的数字
//    Console.WriteLine(chars[index]);
   }
   
   //以下是打印抽取结果的代码
   result.Sort();

   for(int i = 0 ; i < result.Count ; i++)
   {
    if(i !=0)
    {
     if((int)result[i-1] != (int)result[i])
      Console.WriteLine();
    }
    Console.Write("{0}",result[i]);
   }

   Console.ReadLine();
  }
 }
}