题目:已知随机函数rand(),以p的概率产生0,以1-p的概率产生1,现在要求设计一个新的随机函数newRand(), 使其以1/n的等概率产生1~n之间的任意一个数。
解决思路:可以通过已知随机函数rand()产生等概率产生0和1的新随机函数Rand(),然后调用k(k为整数n的二进制表示的位数)次Rand()函数,得到一个长度为k的0和1序列,以此序列所形成的整数即为1--n之间的数字。注意:从产生序列得到的整数有可能大于n,如果大于n的话,则重新产生直至得到的整数不大于n。
第一步:由rand()函数产生Rand()函数,Rand()函数等概率产生0和1。
- int Rand()
- {
- int i1 = rand();
- int i2 = rand();
- if(i1==0 && i2==1)
- return 1;
- else if(i1==1 && i2==0)
- return 0;
- else
- return Rand();
- return -1;
- }
第二步:计算整数n的二进制表示所拥有的位数k,k = 1 +log2n(log以2为底n)
第三步:调用k次Rand()产生随机数。
- int newRand()
- {
- int result = 0;
- for(int i = 0 ; i < k ; ++i)
- {
- if(Rand() == 1)
- result |= (1<<i);
- }
- if(result > n)
- return newRand();
- return result;
- }
题目:
给定一个函数rand5(),该函数可以随机生成1-5的整数,且生成概率一样。现要求使用该函数构造函数rand7(),使函数rand7()可以随机等概率的生成1-7的整数。
思路:
很多人的第一反应是利用rand5() + rand()%3来实现rand7()函数,这个方法确实可以产生1-7之间的随机数,但是仔细想想可以发现数字生成的概率是不相等的。rand()%3 产生0的概率是1/5,而产生1和2的概率都是2/5,所以这个方法产生6和7的概率大于产生5的概率。
正确的方法是利用rand5()函数生成1-25之间的数字,然后将其中的1-21映射成1-7,丢弃22-25。例如生成(1,1),(1,2),(1,3),则看成rand7()中的1,如果出现剩下的4种,则丢弃重新生成。
简单实现:
- int rand7()
- {
- int x = 0;
- do
- {
- x = 5 * (rand5() - 1) + rand5();
- }while(x > 21);
- return 1 + x%7;
- }
备注:
这种思想是基于,rand()产生[0,N-1],把rand()视为N进制的一位数产生器,那么可以使用rand()*N+rand()来产生2位的N进制数,以此类推,可以产生3位,4位,5位...的N进制数。这种按构造N进制数的方式生成的随机数,必定能保证随机,而相反,借助其他方式来使用rand()产生随机数(如 rand5() + rand()%3 )都是不能保证概率平均的。
此题中N为5,因此可以使用rand5()*5+rand5()来产生2位的5进制数,范围就是1到25。再去掉22-25,剩余的除3,以此作为rand7()的产生器。
给定一个函数rand()能产生0到n-1之间的等概率随机数,问如何产生0到m-1之间等概率的随机数?
- int random(int m , int n)
- {
- int k = rand();
- int max = n-1;
- while(k < m)
- {
- k = k*n + rand();
- max = max*n + n-1;
- }
- return k/(max/n);
- }
如何产生如下概率的随机数?0出1次,1出现2次,2出现3次,n-1出现n次?
- int random(int size)
- {
- while(true)
- {
- int m = rand(size);
- int n = rand(size);
- if(m + n < size)
- return m+n;
- }
- }
文章转自:blog.csdn.net/hackbuteer1/article/details/7486748
看到一个更好的描述这个问题的文章,链接地址:
http://www.hawstein.com/posts/19.10.html
相关推荐
java java_leetcode题解之Implement Rand10() Using Rand7().java
225.Implement_stack_using_queues用队列实现栈【LeetCode单题讲解系列】
java java_leetcode题解之Implement Stack using Queues.java
java java_leetcode题解之Implement Stack Using Array.java
java java_leetcode题解之Implement Queue Using Stacks.java
java java_leetcode题解之Implement Queue Using Array.java
By the end of this book, you should be ready to implement advanced, effective and efficient CNN models at your professional project or personal initiatives by working on complex image and video ...
rsa using java to implement and we can encrypt and decrypt files.
1. Implement a blurring filter using the equation (5.6-11,数字图像处理(第三版))in textbook, and blur the test image ‘book_cover.jpg’ using parameters a=b=0.1 and T=1. (20%) 2. Add Gaussian noise ...
python python_leetcode题解之232_Implement_Queue_using_Stacks.py
python python_leetcode题解之225_Implement_Stack_using_Queues.py
232.Implement_Queue_using_Stacks用栈实现队列【LeetCode单题讲解系列】
using jquery implement web shell, so you could use it on your project, to get all the thing you want, and modify the DOM on different browser. now it is the V1 version,in the future , I will make it ...
A console applicaton, such as a telnet emulator, needs to implement a console window for the user to type commands. It is not that easy to code everything that a text editor does.
Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. 的代码
Deep learning simplified by taking supervised, unsupervised, and reinforcement learning to the next level using the Python ecosystem Key Features • Build deep learning models with transfer learning ...
Introduction to Deep Learning Using R: A Step-by-Step ... All examples are taught in the R statistical language, allowing students and professionals to implement these techniques using open source tools.
Using Linux to Implement 8- and 16- Bit Device Networking Solutions
While most C programmers use APIs and the libraries that implement them in almost every application they write, relatively few programmers create and disseminate new, widely applicable APIs....
Using Linux to Implement 8- and 16- Bit Device Networking Solutions.pdf