5.1-1
由于我们可以比较任意两个应聘者,这说明我们可以有一个应聘者的优劣顺序,因此我们知道应聘者的全部次序。
5.1-2
设 n=b−a,我们需要找到最少的 c 位二进制数,使得
1) 调用 c 次RANDOM(0,1),产生
2) 若
3) 返回
生成某个随机数的概率为(12)c,若此数大于 n 则需要重复第 2 步,设第一次生成的数小于等于 RANDOM(0,1),因此有:
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int random_01()
{
static default_random_engine e(time(0));
static bernoulli_distribution b;//伯努利分布
if (b(e))
return 1;
else return 0;
}
int random_ab(int a, int b)
{
int n = b - a;
int c = log(static_cast<double>(n)) / log(static_cast<double>(2)) + 1;
while (true)
{
int res = 0;
for (int i = 0; i != c; ++i)
res += random_01() << i;
if (res + a <= b)
return res + a;
}
}
int main()
{
for (int i = 0; i < 10; ++i)
cout << random_ab(0, 100) << ' ';
cout << endl;
return 0;
}
5.1-3
使用两次BIASED-RANDOM()产生两个随机数 x,
出现两个1的概率为 p2,出现两个0的概率为 (1−p)2,出现一个1和一个0的概率是 2p(1−p)。
E(T)=∑i=1∞2p(1−p)(1−2p(1−p))i−1)=O(12p(1−p))
本文介绍了一种基于二进制随机数生成指定区间内整数的方法,并分析了其时间复杂度。此外,还提供了一个用于生成特定概率下随机数的算法及其期望运行时间。

1931

被折叠的 条评论
为什么被折叠?



