使用一个概率集生成另一个概率集
如何从较小的概率集生成较大的概率集?
这来自算法设计手册 -Steven Skiena
问:
使用以等概率从 {0,1,2,3,4} 生成数字的随机数生成器 (rng04) 编写以等概率生成从 0 到 7 的数字 (rng07) 的随机数生成器?< /p>
我现在尝试了大约 3 个小时,主要是基于对两个 rng04
输出求和。问题是,在这种情况下,每个值的概率是不同的 - 4 出现的概率为 5/24,而 0 出现的概率为 1/24。我尝试了一些方法来掩盖它,但不能。
有人可以解决这个问题吗?
How can I generate a bigger probability set from a smaller probability set?
This is from Algorithm Design Manual -Steven Skiena
Q:
Use a random number generator (rng04) that generates numbers from {0,1,2,3,4} with equal probability to write a random number generator that generates numbers from 0 to 7 (rng07) with equal probability?
I tried for around 3 hours now, mostly based on summing two rng04
outputs. The problem is that in that case the probability of each value is different - 4 can come with 5/24 probability while 0 happening is 1/24. I tried some ways to mask it, but cannot.
Can somebody solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须找到一种方法来组合两组随机数(第一个和第二个随机数
{0,1,2,3,4}
)并制作n*n
独特的可能性。基本上问题是,通过加法,您会得到类似这样的内容,其中有重复项,这不是您想要的。组合这两个集合的一种可能方法是
Z = X + Y*5
,其中X
和Y
是两个随机数。这会给你一组像这样的结果所以现在你有一组更大的随机数,你需要做相反的事情并使其更小。该集合有
25
个不同的值(因为您从 5 开始,并使用了两个随机数,所以5*5=25
)。您想要的集合有 8 个不同的值。一个简单的方法是This 确实有一个
{0,7}
范围。但值{1,7}
将出现 3/25 次,值0
将出现 4/25 次。这是因为0 mod 8 = 0
、8 mod 8 = 0
、16 mod 8 = 0
和24 mod 8 = 0
。要解决此问题,您可以将上面的代码修改为此。
这将采用一个与你的概率无关的值 (
24
) 并将其丢弃。如果你得到一个像这样的“坏”值,生成一个新的随机数将使你的算法运行时间稍微长一些(在这种情况下,1/25 的时间将需要 2 倍的时间来运行,1/625 的时间将需要 3 倍的时间)长等)。但它会给你正确的概率。You have to find a way to combine the two sets of random numbers (the first and second random
{0,1,2,3,4}
) and maken*n
distinct possibilities. Basically the problem is that with addition you get something like thisWhich has duplicates, which is not what you want. One possible way to combine the two sets would be the
Z = X + Y*5
whereX
andY
are the two random numbers. That would give you a set of results like thisSo now that you have a bigger set of random numbers, you need to do the reverse and make it smaller. This set has
25
distinct values (because you started with 5, and used two random numbers, so5*5=25
). The set you want has 8 distinct values. A naïve way to do this would beThis would indeed have a range of
{0,7}
. But the values{1,7}
would appear 3/25 times, and the value0
would appear 4/25 times. This is because0 mod 8 = 0
,8 mod 8 = 0
,16 mod 8 = 0
and24 mod 8 = 0
.To fix this, you can modify the code above to this.
This will take the one value (
24
) that is throwing off your probabilities and discard it. Generating a new random number if you get a 'bad' value like this will make your algorithm run very slightly longer (in this case 1/25 of the time it will take 2x as long to run, 1/625 it will take 3x as long, etc). But it will give you the right probabilities.当然,真正的问题是总和中间的数字(在本例中为 4)以多种组合出现(0+4、1+3 等),而 0 和 8 只有一种方法被生产出来。
我不知道如何解决这个问题,但我会尽力为您减少一点。需要考虑的一些要点:
一种方法是,想象一个根据您的输入构造的以 5 为基数的数字。忽略 44(这是你的第 25 个多余的值;如果你得到它,合成一组新的输入)并取其他的,模 8,你将在 24 个不同的输入组合(每个 3 个)中得到 0-7,这是均等分布。
The real problem, of course, is the fact that the numbers in the middle of the sum (4 in this case) occur in many combinations (0+4, 1+3, etc.) whereas 0 and 8 have exactly one way to be produced.
I don't know how to solve this problem, but I'm going to try to reduce it a bit for you. Some points to consider:
One way to do that is, imagine a number in base 5 constructed from your input. Ignore 44 (that's your 25th, superfluous value; if you get it, synthesize a new set of inputs) and take the others, modulo 8, and you'll get your 0-7 across 24 different input combinations (3 each), which is an equal distribution.
我的逻辑是这样的:
My logic would be this: