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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - zqf620

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