使用一个概率集生成另一个概率集

发布于 2024-08-02 12:00:21 字数 339 浏览 3 评论 0原文

如何从较小的概率集生成较大的概率集?
这来自算法设计手册 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2024-08-09 12:00:21

您必须找到一种方法来组合两组随机数(第一个和第二个随机数 {0,1,2,3,4} )并制作 n*n 独特的可能性。基本上问题是,通过加法,您会得到类似这样的内容,

        X
      0 1 2 3 4

  0   0 1 2 3 4
Y 1   1 2 3 4 5
  2   2 3 4 5 6
  3   3 4 5 6 7
  4   4 5 6 7 8

其中有重复项,这不是您想要的。组合这两个集合的一种可能方法是 Z = X + Y*5,其中 XY 是两个随机数。这会给你一组像这样的结果

        X
       0  1  2  3  4

  0    0  1  2  3  4
Y 1    5  6  7  8  9
  2   10 11 12 13 14
  3   15 16 17 18 19
  4   20 21 22 23 24

所以现在你有一组更大的随机数,你需要做相反的事情并使其更小。该集合有 25 个不同的值(因为您从 5 开始,并使用了两个随机数,所以 5*5=25)。您想要的集合有 8 个不同的值。一个简单的方法是

x = rnd(5)  // {0,1,2,3,4}
y = rnd(5)  // {0,1,2,3,4}
z = x+y*5   // {0-24}
random07 = x mod 8

This 确实有一个 {0,7} 范围。但值 {1,7} 将出现 3/25 次,值 0 将出现 4/25 次。这是因为 0 mod 8 = 08 mod 8 = 016 mod 8 = 024 mod 8 = 0

要解决此问题,您可以将上面的代码修改为此。

do {
  x = rnd(5)  // {0,1,2,3,4}
  y = rnd(5)  // {0,1,2,3,4}
  z = x+y*5   // {0-24}
while (z != 24)

random07 = z mod 8

这将采用一个与你的概率无关的值 (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 make n*n distinct possibilities. Basically the problem is that with addition you get something like this

        X
      0 1 2 3 4

  0   0 1 2 3 4
Y 1   1 2 3 4 5
  2   2 3 4 5 6
  3   3 4 5 6 7
  4   4 5 6 7 8

Which has duplicates, which is not what you want. One possible way to combine the two sets would be the Z = X + Y*5 where X and Y are the two random numbers. That would give you a set of results like this

        X
       0  1  2  3  4

  0    0  1  2  3  4
Y 1    5  6  7  8  9
  2   10 11 12 13 14
  3   15 16 17 18 19
  4   20 21 22 23 24

So 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, so 5*5=25). The set you want has 8 distinct values. A naïve way to do this would be

x = rnd(5)  // {0,1,2,3,4}
y = rnd(5)  // {0,1,2,3,4}
z = x+y*5   // {0-24}
random07 = x mod 8

This would indeed have a range of {0,7}. But the values {1,7} would appear 3/25 times, and the value 0 would appear 4/25 times. This is because 0 mod 8 = 0, 8 mod 8 = 0, 16 mod 8 = 0 and 24 mod 8 = 0.

To fix this, you can modify the code above to this.

do {
  x = rnd(5)  // {0,1,2,3,4}
  y = rnd(5)  // {0,1,2,3,4}
  z = x+y*5   // {0-24}
while (z != 24)

random07 = z mod 8

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.

青春有你 2024-08-09 12:00:21

当然,真正的问题是总和中间的数字(在本例中为 4)以多种组合出现(0+4、1+3 等),而 0 和 8 只有一种方法被生产出来。

我不知道如何解决这个问题,但我会尽力为您减少一点。需要考虑的一些要点:

  • 0-7 范围有 8 个可能的值,因此最终您应该瞄准的可能情况总数必须是 8 的倍数。这样您就可以在其中每个值拥有整数个分布共域。
  • 当您对两个密度函数求和时,可能情况的数量(在计算总和时不一定不同,只是根据输入的不同排列而言)等于每个输入集大小的乘积。
  • 因此,给定两个 {0,1,2,3,4} 集合相加,就有 5*5=25 种可能性。
  • 从 5 的幂中不可能得到 8 的倍数(参见第一点)(参见第二点,但将其外推到任意数量的集合 > 1),因此您需要在您的函数并忽略其中的一些(如果它们发生)。
  • 据我目前所知,最简单的方法是使用两个 {0,1,2,3,4} 集合的总和(25 种可能性)并忽略 1(留下 24,一个倍数)共 8 个)。
  • 因此,现在的挑战已简化为:找到一种方法将剩余的 24 种可能性分配到 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:

  • The 0-7 range has 8 possible values, so ultimately the total number of possible situations that you should aim for has to be a multiple of 8. That way you can have an integral number of distributions per value in that codomain.
  • When you take the sum of two density functions, the number of possible situations (not necessarily distinct when you evaluate the sum, just in terms of different permutations of inputs) is equal to the product of the size of each of the input sets.
  • Thus, given two {0,1,2,3,4} sets summed together, you have 5*5=25 possibilities.
  • It will not be possible to get a multiple of eight (see first point) from powers of 5 (see second point, but extrapolate it to any number of sets > 1), so you will need to have a surplus of possible situations in your function and ignore some of them if they occur.
  • The simplest way to do that, as far as I can see at this point, is to use the sum of two {0,1,2,3,4} sets (25 possibilities) and ignore 1 (to leave 24, a multiple of 8).
  • Thus the challenge now has been reduced to this: Find a way to distribute the remaining 24 possibilities among the 8 output values. For this, you'll probably NOT want to use the sum, but rather just the input values.

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.

抽个烟儿 2024-08-09 12:00:21

我的逻辑是这样的:

rn07 = 0;
do {
  num = rng04;
}
while(num == 4);

rn07 = num * 2;
do {
  num = rng04;
}
while(num == 4);

rn07 += num % 2

My logic would be this:

rn07 = 0;
do {
  num = rng04;
}
while(num == 4);

rn07 = num * 2;
do {
  num = rng04;
}
while(num == 4);

rn07 += num % 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文