先占坑。以后再修改

昨天遇到一道题, Given int Rand(1) =  0或者 1- uniformly distributed,   write a function to implement Rand(29) - uniformly distributed。

由于本科时概率没学好,挂了。回来网上一查资料发现有类似的题目 -  Given Rand(5) = {1, 2, 3, 4, 5}, 求Rand(7)。

求Rand7的代码是

public static int random7() {
int val = (random5() - 1) * 5 + (random5() - 1);
return val < 21 ? val % 7 + 1 : random7();
}

思路其实是把 1 - 5的数字映射到 1-7的范围,只要是uniformly distributed就行, 我们可以不在乎这 1 - 7的概率是 4%, 2 % , 还是 1%, 只要他们相等, 多余的部分我们再舍弃掉。比如计算范围是  1 - 10

  • 我们可以取 1 ,2, 3 ,4 ,5 ,6 ,7,  舍弃 8 - 10 (舍弃的方法是再call一次random函数,程序有可能死循环,但从概率上来看死循环几率极小)
  • 也可以取 4, 5, 6, 7, 8, 9, 10,舍弃 1 - 3,  但计算答案的时候    return val - 3

以上代码是一个解,我们还可以有其他的解。 比如以下,

 public static int random7() {
int val = (random5() - 1) + (random5() - 1) * 5 + (random5() - 1) * 25;
return val < 119 ? val % 7 + 1 : random7();
}

原理就是,只要把random5()得到的数字均匀映射到一串大于7的连续数字里。

所以回到这个题目,Given rand(1)实现 rand(29),其中 rand1() =  0 或者 1,每次只有两个数,所以我们可以使用以下代码来映射到0 - 31, 再舍弃30 和31就可以了:

public static int rand29() {
int val = rand1() + rand1() * 2 + rand1() * 4 + rand1() * 8 + rand1() * 16;
return val < 29 ? val: rand29();
}

2/25/2016

今天又看到有一个公式,假如给定m 和 n以及一个rand(1),那么求一个在m到n中间的数,可以用 rand(1) * (n - m) + n。这里题目不要求uniform distribution的话好像也可以这么做。

Reference:

http://www.growingwiththeweb.com/2014/03/given-random5-implement-random7.html

http://www.careercup.com/question?id=12426697

Random Integer Generator的更多相关文章

  1. 文献翻译|Design of True Random Number Generator Based on Multi-stage Feedback Ring Oscillator(基于多级反馈环形振荡器的真随机数发生器设计)

    基于多级反馈环形振荡器的真随机数发生器设计 摘要 真随机数生成器(trng)在加密系统中起着重要的作用.本文提出了一种在现场可编程门阵列(FPGA)上生成真随机数的新方法,该方法以 多级反馈环形振荡器 ...

  2. react random key generator;react如何产生随机不重复的key

    1.<div key={+new Date() + Math.random()}> 2.使用数组的索引 3.使用uuid:https://www.npmjs.com/package/uui ...

  3. Random Number Generator

    rand()函数可以产生[0,RAND_MAX]之间的均匀的伪随机数,它定义在头文件stdlib.h中,函数原型: int rand(void); C标准库的实现是: unsigned ; /*ran ...

  4. 【Codechef】Random Number Generator(多项式除法)

    题解 前置技能 1.多项式求逆 求\(f(x)\*g(x) \equiv 1 \pmod {x^{t}}\) 我们在t == 1时,有\(f[0] = frac{1}{g[0]}\) 之后呢,我们倍增 ...

  5. @codechef - RNG@ Random Number Generator

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  6. [spojRNG]Random Number Generator

    先将所有数加上Ri,即变为区间[0,2Ri],考虑容斥,将区间容斥为[0,+oo)-[2Ri,+oo),然后对[2Ri,+oo)令$bi=ai-2Ri$,相当于范围都是[0,+oo)问题转化为求n个正 ...

  7. python的random模块

    As an example of subclassing, the random module provides the WichmannHill class that implements an a ...

  8. ISO C Random Number Functions

    This section describes the random number functions that are part of the ISO C standard. To use these ...

  9. Prefer ThreadLocalRandom over Random

    Java 7 has introduced a new random number generator - ThreadLocalRandom Normally to generate Random ...

随机推荐

  1. d3 之值域映射

    <html> <head> <meta charset="utf-8"> <title>d3研究室</title> &l ...

  2. ecshop中无限处理分类

    数据库表记录结构 <?php $sql = "SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, ...

  3. HTML5新增标签的汇总与详解

    趁着一点闲暇时间,把HTML5的新增标签整理了一下,用了表格的形式展现,分别归纳了各标签的用法及属性分析.这样方便各位以后在运用HTML5标记遇到疑惑时,直接上来对照看下就明了了,希望对大家有帮助哦. ...

  4. eclipse安装ermaster建模插件

    下载ermaster.jar 放到plugins重启eclipse即可

  5. aix用命令查监听端口对应的进程

    --aix$netstat -an|grep LISTEN|grep 7867tcp4 0 0 *.7867 *.* LISTEN$netstat -Aan|grep 7867f1000e00029f ...

  6. laravel方法汇总详解

    1.whereRaw() 用原生的SQL语句来查询,whereRaw('select * from user') 就和 User::all()方法是一样的效果 2.whereBetween() 查询时 ...

  7. php截取字符串的实例代码(支持utf-8)

    分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...

  8. Django文档——Model中的ForeignKey,ManyToManyField与OneToOneField

    关联关系字段 (Relationship fields) ForeignKey,ManyToManyField与OneToOneField分别在Model中定义多对一,多对多,一对一关系. 例如,一本 ...

  9. Torry的困惑(基本型)

    #include<stdio.h> int main() { long long i,j; int n; //用于记录输入的要进行乘积的质数的个数 ; //用于记录前n个质数的乘积,并初始 ...

  10. Postgresql 技巧

    备份 pg_dump -f "F:/dump.sql"<file name> -U postgres<database name> -h 10.38.197 ...