一、拒绝采样:
已知rand7()[1..7],求产生rand10()[1..10]
基本思路大家都知道的,就是call rand7()两次然后进行拒绝采样,因此大部分人第一下想到的算法如下:
int rand10() {
int row, col, idx;
do {
row = rand7();
col = rand7();
idx = col + (row-1)*7;//选择第row行第col列的数字,每行7个数字
} while (idx > 40);
return 1 + (idx-1)%10;
}
但问题是有没有必要idx为41~49之间的时候下一次重新调用两次rand7(),答案是没必要,我们可以采用下述算法来进行优化
int rand10Imp() {
int a, b, idx;
while (true) {
a = rand7();
b = rand7();
idx = b + (a-1)*7;
if (idx <= 40)
return 1 + (idx-1)%10;
a = idx-40;
b = rand7();
// get uniform dist from 1 - 63
idx = b + (a-1)*7;
if (idx <= 60)
return 1 + (idx-1)%10;
a = idx-60;
b = rand7();
// get uniform dist from 1-21
idx = b + (a-1)*7;
if (idx <= 20)
return 1 + (idx-1)%10;
}
}
那么接着我们计算方法1和方法2的rand7调用次数的数学期望来证明我们的优化是有效的,期望方程如下:
方法1:
E(# calls to rand7) = 2 * (40/49) +
4 * (9/49) * (40/49) +
6 * (9/49)2 * (40/49) +
...
∞
= ∑ 2k * (9/49)k-1 * (40/49)
k=1
= (80/49) / (1 - 9/49)2
= 2.45
方法2:
E(# calls to rand7) = 2 * (40/49) +
3 * (9/49) * (60/63) +
4 * (9/49) * (3/63) * (20/21) +
(9/49) * (3/63) * (1/21) *
[ 6 * (40/49) +
7 * (9/49) * (60/63) +
8 * (9/49) * (3/63) * (20/21) ] +
((9/49) * (3/63) * (1/21))2 *
[ 10 * (40/49) +
11 * (9/49) * (60/63) +
12 * (9/49) * (3/63) * (20/21) ] +
...
= 2.2123
推广一下,如果需要randN[1..N]生成randM[1..M]怎么做