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

推荐订阅源

S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
W
WeLiveSecurity
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
P
Privacy & Cybersecurity Law Blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
H
Hacker News: Front Page
H
Heimdal Security Blog
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Recorded Future
Recorded Future
阮一峰的网络日志
阮一峰的网络日志
T
Tor Project blog
D
DataBreaches.Net
V
Visual Studio Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
The Hacker News
The Hacker News
A
Arctic Wolf
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
P
Proofpoint News Feed
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
Scott Helme
Scott Helme
酷 壳 – CoolShell
酷 壳 – CoolShell
SecWiki News
SecWiki News
The Cloudflare Blog
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
小众软件
小众软件
博客园 - Franky
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
T
Threat Research - Cisco Blogs

博客园 - 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();
  }
 }
}