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

推荐订阅源

V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Vercel News
Vercel News
C
Check Point Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
P
Proofpoint News Feed
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
IT之家
IT之家
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
Y
Y Combinator Blog
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
T
Tenable Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
A
Arctic Wolf
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
雷峰网
雷峰网
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence

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