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

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - zhengguoqing

新LIMS建设全流程深度调研与实践指南 程序员福利各大平台免费接口非常适用 .netCore调用WebService DotNet 资源大全中文版(Awesome最新版) RAR去除广告 用户能够在下次登录系统时被重新配置---或win10早期更新不成功的bug就需要删除多余的登陆用户 Nuget C#操作Access 简体繁体转换JS(JavaScript)脚本 Google搜索技巧 我的Visual Studio sqlserver获取字段信息 win7取得管理员的权限运行软件 SQL jqModal MSDN资源 Microsoft Security Essentials 不升级方法 office2003安全模式启动,默认模板问题/打开word就显示“无法装载这个对象” SQL Server 2005 集成sp2的 企业版安装后没发现 Management Studio管理工具
已知多个奖品概率,取奖品
zhengguoqing · 2013-02-02 · via 博客园 - zhengguoqing

两种方法: 

1、 定义一个最大区间,如100000,新建一个区间数组,如a[10,110,1110,100000],然后出0~100000的随机数,在哪个区间就是哪个。 注意:区间的差值/最大上限=目标概率    (要判断是哪个区间,只要for循环一下取第一个 a[i]<[随机数i+1 即可)

2、 定义一个大数组,如a[100000],按照概率往里填充目标值,如a[1,1,1, …… 2,2,…… ,5,……],然后打乱(随你怎么打乱都行),再出0~99999的随机数n,取a[n]即可。 注意:所有概率和必须等于1,不要漏掉你不必控制出现 未中奖” 的概率。

第一种方式实现:

        public static int getRandomNum(int[] arr, int[] probablility)
        {
            if (arr.Length!=probablility.Length)
            {
                return -1;
            }
            Random ran =new Random();
            int ran_num = ran.Next(10000);
            int temp = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                temp += probablility[i];
                if (ran_num<temp)
                {
                    return arr[i];
                }
            }
            return -1;
        }

            for (int i = 0; i < 100000; i++)
            {
                Console.Write(getRandomNum(new int[] { 0, 1, 2, 3, 4, 5 }, new int[] { 7878, 1000, 100, 20, 1000, 2 }));
            }