前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode478. Generate Random Point in a Circle

leetcode478. Generate Random Point in a Circle

作者头像
眯眯眼的猫头鹰
发布2019-10-09 15:38:50
4090
发布2019-10-09 15:38:50
举报

题目要求

代码语言:javascript
复制
Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

1. input and output values are in floating-point.
2. radius and x-y position of the center of the circle is passed into the class constructor.
3. a point on the circumference of the circle is considered to be in the circle.
4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:
Input: 
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:
Input: 
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution's constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren't any.

假设现在已知圆的圆心的x和y坐标,以及该圆的半径radius。要求写一个随机点生成器,要求该生成器生成的点必须在圆内,且每一个点被生成的概率为相等的。规定圆周上的点也属于圆内。

思路1:Rejection Sampling

该思路很简单,即取能够容下该圆的最小正方形,并且随机生成该正方形内的点。如果点不在圆内,则继续重新生成。正方形内等概率的随机点很好生成,可以直接利用JAVA内置的随机数生成器即可。x坐标的随机数范围为[x-radius, x+radius], y坐标的随机数范围为[y-radius, y+radius]。代码如下:

代码语言:javascript
复制
    public double[] randPoint2() {
        double x0 = x_center - radius;
        double y0 = y_center - radius;
        while(true) {
            double xg = x0 + Math.random() * radius * 2;
            double yg = y0 + Math.random() * radius * 2;
            if (Math.pow((xg - x_center) , 2) + Math.pow((yg - y_center), 2) <= radius * radius)
                return new double[]{xg, yg};
        }
    }

思路二:极坐标

假如我们能够利用极坐标的思想,先从(0, 360]度之间生成一个随机的角度,再在[0, radius]之间生成一个随机的长度,理论上就可以了。但是通过这种方式生成的随机点会明显的在靠近圆心的位置密度更大,如下图所示(图片来源于网络):

clipboard.png
clipboard.png

究其原因在于,从圆心向圆周方向的半径扩展出去,单位长度所构成的圆环的面积并非相等的。如下图(图片来源于网络):

clipboard.png
clipboard.png

假设将圆周拆分为等分的3部分,则最内圈的面积为

clipboard.png
clipboard.png

,中圈的面积为

clipboard.png
clipboard.png

=3A,同理外圈的面积为5A。如果按照半径上的每一个点都是等概率生成的话,会发现内圈因为面积更小,导致点的密度更大。但是由此也可以看出,第i圈的面积和单位面积成线性正相关。

clipboard.png
clipboard.png

接着要先介绍两个概率论中的概念:概率密度函数(probability density function 简称pdf)累计分布函数(Cumulative Distribution Function 简称 cdf)

概率密度函数是指某个随机数生成器生成的数字在某个点附近的概率值。比如现在有一个随机数生成器能够等概率生成[a,b]区间的任意一个数,则其pdf(x)= 1/(b-a)

累计分布函数对离散变量而言,所有小于等于a的值出现概率的和。还是以[a,b]区间的等概率随机数生成器为例,cdf(x)表示随机数生成器生成的数位于[a,x]区间内的概率,其值为cdf(x)=(x-a)/(b-a) a<=x<=b

可以看到cdf其实是对pdf在该区间上进行微积分计算的结果。

从这题的角度而言,既然已知随着r向着半径增加,在该位置上生成随机数的概率为线性增加,因此可以设

clipboard.png
clipboard.png

,其中a为概率值。因为已知生成的所有点的必定位于[0,R]之上,因此cdf(R)=

clipboard.png
clipboard.png

,由此可以得出

clipboard.png
clipboard.png

。再将a的值带回原式中,可以得出

clipboard.png
clipboard.png

。在对pdf(r)进行积分计算可以得出

clipboard.png
clipboard.png

。再次校验可以看到cdf(R)=1确实成立。

但是,JAVA只能提供一种通用的随机数生成器,即在一定区间内每一个数均为等概率出现。对于这种随机数生成器,其cdf(x)=x,即生成[0,x]之间的随机数的概率为r。将二者相等即可得出如下结论

clipboard.png
clipboard.png

代码如下:

代码语言:javascript
复制
    public double[] randPoint() {
        double len= Math.sqrt(Math.random())*radius;
        double deg= Math.random()*2*Math.PI;
        double x= x_center+len*Math.cos(deg);
        double y= y_center+len*Math.sin(deg);
        return new double[]{x,y};
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目要求
  • 思路1:Rejection Sampling
  • 思路二:极坐标
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档