Random Integer Generator

先占坑。以后再修改

昨天遇到一道题, 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();
}

Reference:

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

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

时间: 2024-08-03 23:26:30

Random Integer Generator的相关文章

C++ 利用&lt;cstdlib&gt; 头文件 , 产生一个random number generator

头文件<cstdlib>中有一个重要的函数 rand() , 可以作为随机数发生器. 现在现在我想产生一个随机数, 我使用如下的程序: #include <iostream> #include <ctsdlib> <span style="font-family: Arial, Helvetica, sans-serif;">using namespace std;</span> int main() {     cout &

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

1.<div key={+new Date() + Math.random()}> 2.使用数组的索引 3.使用uuid:https://www.npmjs.com/package/uuid 4.使用uniqid:https://www.npmjs.com/package/uniqid 5.Date.now() 原文地址:https://www.cnblogs.com/shengulong/p/10122759.html

Linux Apache2.4 AH00141: Could not initialize random number generator

检查编译安装步骤,如果是采用下面这个方法安装,请换一种方法,我遇到过多次这个问题,都是因为这个安装问题导致的. 存在问题的方法 解压apr apr-util,去掉版本号,拷贝至httpd-<version>/srclib/ ./configure --prefix=/usr/local/apache2 --enable-dav --enable-so --enable-mods-shared=all --with-egd --with-devrandom --enable-so --enabl

@codechef - [email&#160;protected] Random Number Generator

目录 @[email protected] @[email protected] @part - [email protected] @part - [email protected] @part - [email protected] @accepted [email protected] @[email protected] @[email protected] 给定递推关系式:\[A_i=C_1A_{i-1} + C_2A_{i-2}+\dots+C_kA_{i-k}\] 并给定 \(A_

OpenCV Tutorials &mdash;&mdash; Random generator and text with OpenCV

creating a Random Number Generator object (RNG): RNG rng( 0xFFFFFFFF ); 创建并初始化随机数生成子 create a matrix initialized to zeros (which means that it will appear as black), specifying its height, width and its type: /// Initialize a matrix filled with zeros

python的random模块

As an example of subclassing, the random module provides the WichmannHill class that implements an alternative generator in pure Python. The class provides a backward compatible way to reproduce results from earlier versions of Python, which used the

Google APAC Test 2016 Not So Random 矩阵快递幂(logN复杂度)

Problem There is a certain "random number generator" (RNG) which takes one nonnegative integer as input and generates another nonnegative integer as output. But you know that the RNG is really not very random at all! It uses a fixed number K, an

python标准库-random学习

参考资料:Python 2.7.7 documentation 参考工具:http://translate.google.cn/ random模块学习 一.Bookkeeping functions(几乎没看懂)     random.seed([x]) Initialize the basic random number generator     random.getstate() Return an object capturing the current internal state o

标准库random

random - Random variable generators. 随机变量生成器 class Random(_random.Random) -- Random number generator base class used by bound module functions. 以下是所有的random的方法: 1 import random 2 print(random.random()) #生成一个0-1之间的浮点数随机数[0,1] 3 print(random.randint(1,