Leetcode 915 Solution

This article provides solution to leetcode question 915 (generate-random-point-in-a-circle)

https://leetcode.com/problems/generate-random-point-in-a-circle

Solution

class Solution:
def __init__(self, radius: float, x_center: float, y_center: float): self.radius = radius self.x = x_center self.y = y_center
def randPoint(self) -> List[float]: while True: x = random.uniform(self.x - self.radius, self.x + self.radius) y = random.uniform(self.y - self.radius, self.y + self.radius)
if (x - self.x)**2 + (y - self.y) ** 2 > self.radius**2: continue
return [x, y]
# Your Solution object will be instantiated and called as such: # obj = Solution(radius, x_center, y_center) # param_1 = obj.randPoint()