【问题标题】:Random point in circle method is not uniformly distributed [duplicate]圆方法中的随机点不是均匀分布的[重复]
【发布时间】:2017-08-02 03:04:24
【问题描述】:

我在Java中有以下方法:

public static Vector2d random(Circle circle) {
    // this returns a random number between 0 and Math.PI * 2
    double angle = MathUtils.random(0, Math.PI * 2);
    // give the point inside the unit circle
    // this returns a normalized vector from a given angle
    Vector2d point = new Vector2d(angle);
    // however, this is only along the edge
    // now add a random magnitude (because this is a normalized vector, we can just multiply it by the desired magnitude)
    double magnitude = Math.random();
    point = point.multiply(magnitude);
    // now expand this to fit the radius
    point = point.multiply(circle.getRadius());
    // now translate by circleCenter
    return point.add(circle.getCenter());
}

这确实会在定义的圆圈中返回一个点,但是,当您多次执行此操作并绘制点时,您可以清楚地看到大多数点将朝向中心。

这是为什么?我不明白我的数学怎么能做到这一点。

如果您希望我在情节上添加点的图像,请发表评论,如果您认为这可能会有所帮助。

【问题讨论】:

  • 是的,发生这种情况的原因大致相同,即两极附近的经线比赤道附近的经线更近。一个在圆中得到均匀分布的随机点的好方法是在外接正方形中得到随机点,然后拒绝那些落在圆外的点。
  • 试试这个:画一个圆圈(或者想象你正在画一个圆圈)。像切披萨一样分成 16 等份。现在,对于从中心到外边缘的每个半径,将线段分成 10 个相等的部分,并沿半径放置 10 个均匀分布的点。我想你会注意到这些点在中心附近似乎更加集中。这实际上就是您的随机化方法正在做的事情。
  • @ajb 哦,谢谢,这帮助我将其可视化。现在我明白为什么会这样了

标签: java math random geometry uniform-distribution


【解决方案1】:

当然,当r 较小时,生成的点彼此更接近。

正如@DBrowne 所说,您可以通过逆 CDF 技巧调整密度。

或者,您可以通过在[-R,R]x[-R,R] 中绘制统一点并拒绝X²+Y²>R² 中的点(约占其中的 21%)来节省函数评估。该方法可以推广到任何由其隐式方程已知的形状。

【讨论】:

    【解决方案2】:

    你的数学有问题。以下是原因和正确解决方案的解释:

    任务是在一个圆圈内生成均匀分布的数字 在 (x,y) 平面上的半径为 R。起初极坐标看起来像 一个好主意,天真的解决方案是统一选择半径 r 分布在 [0, R] 中,然后是角度 theta 均匀分布 在 [0, 2pi] 中。但是,您最终会在原点附近获得大量点 (0, 0)!这是错误的,因为如果我们看某个角度区间, 说[theta, theta+dtheta],需要生成更多的点 更远(在大 r 处),而不是接近于零。半径不得为 从一个均匀分布中挑选出来的,但一个是这样的

    pdf_r = (2/R^2)*r

    通过计算累积的倒数很容易做到这一点 分布,我们得到 r:

    r = R*sqrt(rand())

    其中 rand() 是 [0, 1] 中的均匀随机数

    http://www.anderswallin.net/2009/05/uniform-random-points-in-a-circle-using-polar-coordinates/

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2012-06-08
      • 2021-12-28
      相关资源
      最近更新 更多