先占坑。以后再修改

昨天遇到一道题, 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. [设计模式]Netd中的命令设计模式

    命令模式 有如下的角色: (1)调用者(invoker) (2)命令接收者(receiver) (3)客户端(client) (4)命令对象(command) public interface Com ...

  2. java学习笔记_MIDI

    import javax.sound.midi.*; public class Midi { public void play(int instrument, int note) { try { Se ...

  3. 1_jz2440在linux下烧写裸机程序

    常用的烧写方法有: 1.使用并口工具烧写:接线(参考百问网JZ2440V2开发板使用手册),使用oflash烧写(速度比较慢),可烧写.bin文件,从新上电观察效果.可烧写u_boot. 2.使用op ...

  4. SublimeText更换皮肤

    SublimeText是一款非常好用的文本编辑工具,官方网址http://www.sublimetext.com/. 这里介绍一下手动安装SublimeText皮肤的方法. (1)首先找一款你喜欢的皮 ...

  5. sublime text2之js压缩-Js Minifier

    一款基于Google Closure compiler压缩Js文件插件. 快捷键: Ctrl+Alt+M            当前文件内压缩Js代码(不推荐) Ctrl+Alt+Shift+M   ...

  6. Java执行命令行脚本

    百度到的 Process p=Runtime.getRuntime().exec("C:\\test.cmd"); ProcessBuilder processBuilder=ne ...

  7. 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。

    看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...

  8. 九度OJ1172--哈夫曼树

    哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有多组数据.每 ...

  9. delphi 仅带下划线的TEdit控件

    在做录入框的时候,很希望有一个只带下划线的文本框,网上介绍的很多,有自己做组件的,须不知Delphi下只需要简单设置几个属性即可达到目的.

  10. Python字符串内建处理函数

    #coding=utf-8 __author__ = 'Administrator' # 字符串处理函数 s1 = "study python string function , I lov ...