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. 读取手机联系人,并用listview显示

    读取手机联系人,用到的就是一个contentprovider. 数据库里面有三张重要的表 raw_contact 里面有所有联系人的数据 data 每个联系人的所有数据 mime-type 每条数据的 ...

  2. Python学习之正则表达式初探

    正则表达式 正则表达式 (或 regexes ) 是通用的文本模式匹配的方法. Django URLconfs 允许你 使用任意的正则表达式来做强有力的URL映射,不过通常你实际上可能只需要使用很少的 ...

  3. leetcode 【 Pascal's Triangle 】python 实现

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

  4. IOS开发学习笔记025-xib和storyboard

    stotyboard : 描述软件界面,大范围,比较适合整个软件的所有界面 xib文件的使用:描述软件界面,小范围,比较适合描述小界面 在xcode新建文件窗口可以看到两个文件,storyboard和 ...

  5. ogre3D学习基础4 -- 网格工具与硬件缓存

    三.网格工具(Mesh) 1.导出器(Exporters)--- 用于从模型生成器中得到数据并且导入到OGRE中去. 导出器是指通过3D模型工具的插件写成网格数据和骨骼动画的文件格式可以在OGRE中被 ...

  6. Python+Selenium练习篇之3-利用tag name定位元素

    前一篇文章介绍了如何通过元素的id值来定位web元素,本文介绍如何通过tag name来定位元素.个人认为,通过tag name来定位还是有很大缺陷,定位不够精确.主要是tag name有很多重复的, ...

  7. python - 接口自动化测试 - TestLogin - 登录接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...

  8. vlc无法播放.flv视频文件

    解决方法:https://videoconverter.wondershare.com/vlc/flv-not-displaying-video-vlc-media-player.html. 在pre ...

  9. [python][django学习篇][11]后台admin用户登录博客,添加文章---这一章和博客首页设计没有关系

    1 如果没有创建超级管理员账号,先要创建python manage.py createsuperuser 2 在admin后台注册模型(如果没有这一步,登录http://127.0.0.1:8000/ ...

  10. BZOJ 1015:[JSOI2008]星球大战starwar(逆向处理+并查集)

    [JSOI2008]星球大战starwar                                                时间限制: 3 Sec 内存限制: 162 MB[题目描述] ...