采摘基于概率的随机项目 [英] Picking a random item based on probabilities

查看:196
本文介绍了采摘基于概率的随机项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个<一个href=\"http://stackoverflow.com/questions/2649717/c-function-for-picking-from-a-list-where-each-element-has-a-distinct-probabilit\">similar问题,我知道,但我感到困惑,所以我认为这更容易在我的方式问。

There's a similar question, I know, but it confused me, so I thought it easier to ask in my way.

所以,我有价值观,正面和负面的数组。越高它们,它们有被选择的多概率。结果
我无法真正弄清楚如何分配的概率,然后随机选择一个。我猜阵列将需要先进行排序,但后来我有点后丢失。

So I have an array of values, positive and negative. The higher they are, the more probability they have of being chosen.
I'm having trouble actually figuring out how to assign the probabilities and then randomly choose one. I'm guessing the array will need to be sorted first, but then I'm a bit lost after that.

推荐答案

我有杯咖啡的各种不同的大小。他们是越大,我就越想为他们充电。我无法真正搞清楚如何分配的价格。

"I have various different sizes of cups of coffee. The larger they are, the more I want to charge for them. I'm having trouble actually figuring out how to assign prices".

这不仅是一个规划问题 - 您指定使用值的概率增大,但你没有说的如何的它的价值增加。通常,咖啡店不成正比的咖啡的量充电。您的无法的比例值赋给概率,因为你的一些值是负的,但概率不能为负数。

This isn't just a programming problem - you've specified that probability increases with value, but you haven't said how it increases with value. Normally, coffee shops don't charge in direct proportion to the amount of coffee. You can't assign probabilities in proportion to value, because some of your values are negative, but probabilities cannot be negative.

听起来像是你需要明确的问题有点多前,你可以写任何code。

Sounds like you need to nail down the problem a bit more before you can write any code.

如果你真的不在乎概率如何与价值,除了他们在价值次序增加,那么一个简单的方法是:

If you really don't care how probability relates to value, other than that they increase in order of value, then one easy way would be:


  • 排序阵列

  • 分配概率为1到第一元件,第2至第二,等等。

  • 现在,你的概率加起来还不到1,这是一个问题。 (1 + 2 + ... + N)= N(N + 1)/ 2 :那么按总您已经分配了所有的概率将每个概率。这就是所谓的正常化。

  • sort your array
  • assign a probability of 1 to the first element, 2 to the second, and so on.
  • now, your probabilities don't add up to 1, which is a problem. So divide each probability by the total of all the probabilities you have assigned: (1 + 2 + .. + n) = n(n+1)/2. This is called "normalization".

鉴于你的概率,这加起来1的列表,重复选一个最简单的方法通常是计算的累积概率的,我会用一个例子来演示:

Given your list of probabilities, which add up to 1, the easiest way to repeatedly choose one is generally to calculate the cumulative probabilities, which I will demonstrate with an example:

value (sorted):           -12     -3      127    1000000
assigned probability:     0.1     0.2     0.3      0.4
cumulative probability:   0.1     0.3     0.6      1.0

的累积概率被定义为所有的概率到这一点的总和。

The cumulative probability is defined as the sum of all the probabilities up to that point.

现在,从你的随机数发生器需要在0和1之间的随机(浮点)值如果在0到0.1之间,你选择了-12。如果它位于0.1和0.3之间,你找-3,等等。为了弄清楚哪些范围还在于,你可以通过你的线性阵列行走,你可以做一个二进制搜索。

Now, from your random number generator you need a random (floating-point) value between 0 and 1. If it lies between 0 and 0.1, you've picked -12. If it lies between 0.1 and 0.3, you've picked -3, and so on. To figure out which range it lies in, you could walk linearly through your array, or you could do a binary search.

您可以跳过正常化的步骤和使用浮点的,如果你想要的。分配的累积概率(1,3,6,10 ...),但要理解的是,实际的概率是所存储的整数值由N(N + 1)/ 2除以。然后,从选择0到n的随机整数(N + 1)/ 2 - 1。如果是小于1,您所选择的第一个值,否则,如果小于3第二个,依此类推。这可能会或可能不会使code清晰,你的RNG可能会或不会做的很好,从大范围选择的整数值。

You could skip the normalization step and the use of floating-point, if you wanted. Assign "cumulative probabilities" (1, 3, 6, 10 ...) , but make it understood that the actual probability is the stored integer value divided by n(n+1)/2. Then choose a random integer from 0 to n(n+1)/2 - 1. If it's less than 1, you've selected the first value, else if less than 3 the second, and so on. This may or may not make the code clearer, and your RNG may or may not do well choosing integer values from a large range.

请注意,你可能分配的概率(0.001,0.002,0.003,0.994),而不是(0.1,0.2,0.3,0.4),仍然满足您的要求,即值越高,概率就越高。

Note that you could have assigned probabilities (0.001, 0.002, 0.003, 0.994) instead of (0.1, 0.2, 0.3, 0.4), and still satisfied your requirement that "the higher the value, the higher the probability".

这篇关于采摘基于概率的随机项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆