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的更多相关文章

  1. Some series and integrals involving the Riemann zeta function binomial coefficients and the harmonic numbers

    链接:http://pan.baidu.com/s/1eSNkz4Y

  2. ORACLE_TO_CHAR Function

    TECHONTHENNTE  WEBSITE: https://www.techonthenet.com/oracle/functions/to_char.php Oracle / PLSQL: TO ...

  3. [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 ...

  4. JavaScript中‘this’关键词的优雅解释

    本文转载自:众成翻译 译者:MinweiShen 链接:http://www.zcfy.cc/article/901 原文:https://rainsoft.io/gentle-explanation ...

  5. VB6+Winsock编写的websocket服务端

    早就写好了,看这方面资料比较少,索性贴出来.只是一个DEMO中的,没有做优化,代码比较草.由于没地方上传附件,所以只把一些主要的代码贴出来. 这只是服务端,不过客户端可以反推出来,其实了解了webso ...

  6. 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 ...

  7. 关于Kendo的Grid 单元格样式

    <!DOCTYPE html><html style="height: 100%;"><head><meta http-equiv=&qu ...

  8. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  9. 99 Lisp Problems 列表处理(P1~P28)

    L-99: Ninety-Nine Lisp Problems 列表处理类问题的解答,用Scheme实现,首先定义几个在后续解题中用到的公共过程: ; common procedure (define ...

随机推荐

  1. WPF异步回调时回调函数如何获取异步函数产生的变量

    有这么一个问题,WPF在使用异步回调的时候,回调函数需要用到异步函数里产生的一个变量,例如异步函数里查询数据库得到了一个DataTable,如何传递给回调函数呢? [方案一]使用全局变量 很容易想到的 ...

  2. Service IntentService区别 (面试)

    依然记得自己当初没有真正的工作经验的时候的日子,满北京跑,没有人要.妈的,现在就想问,还有谁!想想真解气.不提了. 曾经有个面试官问我service 和IntentService的区别.当时自己模模糊 ...

  3. 03013_动态页面技术-JSP

    1.jsp的出现 2.jsp脚本和注释 (1)jsp脚本 ①<%java代码%> ----- 内部的java代码翻译到service方法的内部: ②<%=java变量或表达式> ...

  4. JS 小数处理

    parseInt(7/2);//丢弃小数部分,保留整数部分 Math.ceil(7/2);//向上取整 Math.floor(7/2);//向下取整 Math.round(7/2);//四舍五入 // ...

  5. 分分钟教你做出自己的新闻阅读APP

    分分钟教你做出自己的新闻阅读APP 引子 曾经不小心发现了一些好的看新闻的网站,但是电脑又不是随身携带,因此想要下载一个这个网站的手机APP来看新闻,但是问题来了,这个网站根本没有做Android端! ...

  6. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  7. 安恒月赛 image up

    http://101.71.29.5:10007/index.php?page=login 仔细观察这个url的话会发现,存在文件包含. 而且并没有login.php而是login,猜测代码是 < ...

  8. [oldboy-django][3作业汇总]登录,注册最终版

    # 作业(登录,注册)最终版 - 保留上次输入的值 - 用户数据格式的验证

  9. layer 体验

    做后端的,前端一直不擅长,提示语以前也只会alert,非常难看,后来在别人代码看到lay.msg() https://pan.baidu.com/s/1eRHH59g <!DOCTYPE htm ...

  10. pat 甲级 1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...