这里采用一个极坐标的算法。首先随机出一个圆,与给定的圆同心,但是我们知道一个点落在小圆的概率与大圆的概率比值是面积的比值。
然后随机一个角度出来,0~2PI。
利用x=R*sin(t),y=R*cos(t)得到最终结果。
import random
import math
class Solution:
def __init__(self, radius: float, x_center: float, y_center: float):
self.r=radius
self.x0=x_center
self.y0=y_center
def randPoint(self):
rr=self.r*math.sqrt(random.random())
theat=random.random()*2*math.pi
xr=rr*math.sin(theat)
yr=rr*math.cos(theat)
return [xr+self.x0,yr+self.y0]