Write a function that generates one of 3 numbers according to given probabilities
You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclusive. Generate 3 numbers x, y, z with probability P(x), P(y), P(z) such that P(x) + P(y) + P(z) = 1 using the given rand(a,b) function.
The idea is to utilize the equiprobable feature of the rand(a,b) provided. Let the given probabilities be in percentage form, for example P(x)=40%, P(y)=25%, P(z)=35%..
// This function generates 'x' with probability px/100, 'y' with
// probability py/100 and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
int random(int x, int y, int z, int px, int py, int pz) {
// Generate a number from 1 to 100
int r = rand(, ); // r is smaller than px with probability px/100
if (r <= px)
return x; // r is greater than px and smaller than or equal to px+py
// with probability py/100
if (r <= (px+py))
return y; // r is greater than px+py and smaller than or equal to 100
// with probability pz/100
else
return z;
}
Write a function that generates one of 3 numbers according to given probabilities的更多相关文章
- Some series and integrals involving the Riemann zeta function binomial coefficients and the harmonic numbers
链接:http://pan.baidu.com/s/1eSNkz4Y
- ORACLE_TO_CHAR Function
TECHONTHENNTE WEBSITE: https://www.techonthenet.com/oracle/functions/to_char.php Oracle / PLSQL: TO ...
- [Javascript] Write a Generator Function to Generate the Alphabet / Numbers
When you need to generate a data set, a generator function is often the correct solution. A generato ...
- JavaScript中‘this’关键词的优雅解释
本文转载自:众成翻译 译者:MinweiShen 链接:http://www.zcfy.cc/article/901 原文:https://rainsoft.io/gentle-explanation ...
- VB6+Winsock编写的websocket服务端
早就写好了,看这方面资料比较少,索性贴出来.只是一个DEMO中的,没有做优化,代码比较草.由于没地方上传附件,所以只把一些主要的代码贴出来. 这只是服务端,不过客户端可以反推出来,其实了解了webso ...
- zenefits oa - random(5) to generate a random(7)
If given a function that generates a random number from 1 to 5, how do you use this function to gene ...
- 关于Kendo的Grid 单元格样式
<!DOCTYPE html><html style="height: 100%;"><head><meta http-equiv=&qu ...
- 词频统计_输入到文件_update
/* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...
- 99 Lisp Problems 列表处理(P1~P28)
L-99: Ninety-Nine Lisp Problems 列表处理类问题的解答,用Scheme实现,首先定义几个在后续解题中用到的公共过程: ; common procedure (define ...
随机推荐
- 笔记-爬虫-robots.txt
笔记-爬虫-robots.txt 1. robots.txt文件简介 1.1. 是什么 robots.txt是用来告诉搜索引擎网站上哪些内容可以被访问.哪些不能被访问.当搜索引擎访问一 ...
- MySQL之Schema与数据类型优化
选择优化的数据类型 MySQL支持的数据类型非常多,选择正确的数据类型对于获得高性能至关重要.不管存储哪种类型的数据,下面几个简单的原则都有助于做出更好的选择: 更小的通常更好一般情况下,应该尽量使用 ...
- MySQL基础6-分组查询
1.分组函数 需求20:查询所有商品平均零售价SELECT AVG(salePrice) FROM product 需求21:查询商品总记录数SELECT COUNT(id) count FROM p ...
- laravel5.2总结--redis使用
一切的前提都是已经安装好了redis服务器,并且能启动(我只总结了mac的安装方法:传送门) 我自己使用的是mac系统,有个教程可以参考下,传送门: 1.安装PHP PRedis 1>PRedi ...
- Three Steps to Migrate Group Policy Between Active Directory Domains or Forests Using PowerShell
Three Steps Ahead Have you ever wished that you had three legs? Imagine how much faster you could ru ...
- RSA进阶之共模攻击
适用场景: 同一个n,对相同的m进行了加密,e取值不一样. e1和e2互质,gcd(e1,e2)=1 如果满足上述条件,那么就可以在不分解n的情况下求解m 原理 复杂的东西简单说: 如果gcd(e1, ...
- 看似不是dfs的dfs HDU-1455
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- angular2 表单的理解
angular2表单分为两种,一种为模板驱动,一种为模型驱动: 个人理解两者的不同 模板驱动依靠H5规则进行验证,在提交表单时进行自定义验证: 模型驱动在加载时候已经加载了所有的验证自定义验证,所以不 ...
- HTML5 新增绘图功能
<!DOCTYPE html> <html> <head lang="en"> <title></title> < ...
- BZOJ4872 [六省联考2017]分手是祝愿 【期望dp】
题目 Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为 从 1 ...