【LeetCode】478. Generate Random Point in a Circle 解題報告(Python)
題目描述:
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:
- input and output values are in floating-point.
- radius and x-y position of the center of the circle is passed into the class constructor.
- a point on the circumference of the circle is considered to be in the circle.
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.
題目大意
給定圓的圓心和半徑,生成落在這個圓裡面的隨機點。
解題方法
比較簡單的方法是拒絕取樣(Rejection Sampling),先在圓的外接圓內隨機採點,然後判斷這個點是不是在圓內,如果是的話就返回,否則再取一次。這個方法還可以用來估計圓周率。方法就不寫了。。
我直覺的方法還是使用極座標的方式,隨機找半徑、隨機找角度,然後就確定了一個點。但是沒有通過最後一個測試用例。如果不查資料我是不會發現,這個做法是錯的……
直接對半徑和角度進行隨機取樣的方式會使得靠近圓心的點被取到的概率偏大,而在這個圓內不是均勻的。為什麼呢?因為題目要求的是對圓內任何面積上的點是均勻分佈的,而不是對圓心出發的線上是均勻分佈的。那麼以圓心為半徑的小圓的範圍內的點密度一定會比更大圓的密度大。
時間複雜度是O(1),空間複雜度是O(1)。
class Solution:
def __init__(self, radius, x_center, y_center):
"""
:type radius: float
:type x_center: float
:type y_center: float
"""
self.r = radius
self.x = x_center
self.y = y_center
def randPoint(self):
"""
:rtype: List[float]
"""
nr = math.sqrt(random.random()) * self.r
alpha = random.random() * 2 * 3.141592653
newx = self.x + nr * math.cos(alpha)
newy = self.y + nr * math.sin(alpha)
return [newx, newy]
# Your Solution object will be instantiated and called as such:
# obj = Solution(radius, x_center, y_center)
# param_1 = obj.randPoint()
參考資料:
日期
2018 年 10 月 14 日 —— 周賽做出來3個題,開心