加权随机图

发布于 2024-10-18 00:15:46 字数 90 浏览 2 评论 0原文

假设我有一个大的二维数组,其值范围在 [0,1] 范围内,其中 0 表示“不可能”,1 表示“极有可能”。

如何根据上述概率在该数组中选择一组随机点?

Suppose I have a big 2D array of values in the range [0,1] where 0 means "impossible" and 1 means "highly probable".

How can I select a random set of points in this array according to the probabilities described above ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

东走西顾 2024-10-25 00:15:46

看待问题的一种方法是(暂时)忽略您正在处理二维网格的事实。你拥有的是一组加权的项目。从这样的集合中随机选择的标准方法是:

  1. 对权重求和,将总和称为s
  2. 选择一个统一的随机值0 <= u << s
  3. 遍历项目,保持您检查过的项目权重的运行总 t
  4. 一旦 t >= u,选择您当前正在查看的物品(您刚刚添加了重量的物品)。

可以通过添加以下步骤进行修改,以进行多项选择而无需替换:

  • 每次选择后,从 s 中扣除所选项目的重量(如果您的重量是浮点且稳定性是问题,您可能更愿意从头开始重新计算,至少偶尔)。

  • 从 2 开始重复,但在步骤 3 中跳过先前选择的项目。

如果对权重求和不可行或不受欢迎(如果您的数组特别大,则可能是这样),还有其他选择。第一个想到的是拒绝抽样,这是一个相当广泛的主题,所以我只会向您推荐有关该主题的谷歌和维基百科,因为它们的覆盖范围非常好。

编辑:忘记回到你有一个二维数组的事实。您可以通过预先计算(MIPMAP 样式)地图中区域层次结构的权重总和来显着加快速度,这样您就可以快速跳到实际选定权重的位置。

One way to look at the problem is to ignore (for the moment) the fact that you're dealing with a 2d grid. What you have are a set of weighted items. A standard way of randomly selecting from such a set is to:

  1. sum the weights, call the sum s
  2. select a uniform random value 0 <= u < s
  3. iterate through the items, keeping a running total t of the weights of the items you've examined
  4. as soon as t >= u, select the item you're currently looking at (the one whose weight you just added).

This can be modified to make multiple selections without replacement by adding the following steps:

  • After each selection, deduct the weight of the selected item from s (if your weights are floating point and stability is an issue, you might prefer to recalculate it from scratch, at least occasionally).

  • repeat from 2, but in step 3 skip items that have been previously selected.

If summing the weights is infeasible or undesirable (which it may be if your array is particularly large) there are other options. The first that comes to mind is rejection sampling, which is a fairly broad topic so I'll just refer you to google and wikipedia on that one, as their coverage is pretty good.

EDIT: Forgot to come back to the fact that you have a 2D array. You can speed things up significantly by pre-computing (MIPMAP-style) the sums of weights of a hierarchy of regions in the map, so you can skip quickly to the location of the actual selected weight.

情未る 2024-10-25 00:15:46

代码:

  count = 0
  maxPointsInSet = 100
  foreach(point in array){
      if(randomWithChacnce(point.value))) {
         addToSet(point)
         count++
      }
      if(count == maxPointsInSet)
         break;
  }

  function randomWithChacnce(int num){
    random = a randomized number between 0 to 1 // or random from 1 to 100 num*100
    if(num >= random)
     return true;
    return false
  }

如果您需要任何特定语言的版本,请询问我

the code:

  count = 0
  maxPointsInSet = 100
  foreach(point in array){
      if(randomWithChacnce(point.value))) {
         addToSet(point)
         count++
      }
      if(count == maxPointsInSet)
         break;
  }

  function randomWithChacnce(int num){
    random = a randomized number between 0 to 1 // or random from 1 to 100 num*100
    if(num >= random)
     return true;
    return false
  }

if u need it in any specific language just ask me

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