Given the radius and the position of the center of a circle, implement the function randPoint
which generates a uniform random point inside the circle.
Implement the Solution
class:
Solution(double radius, double x_center, double y_center)
initializes the object with the radius of the circle radius
and the position of the center (x_center, y_center)
.randPoint()
returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array [x, y]
.
Example 1:
Input ["Solution", "randPoint", "randPoint", "randPoint"] [[1.0, 0.0, 0.0], [], [], []] Output [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]] Explanation Solution solution = new Solution(1.0, 0.0, 0.0); solution.randPoint(); // return [-0.02493, -0.38077] solution.randPoint(); // return [0.82314, 0.38945] solution.randPoint(); // return [0.36572, 0.17248]
Constraints:
0 < radius <= 108
-107 <= x_center, y_center <= 107
3 * 104
calls will be made to randPoint
.This problem asks to generate random points within a circle. The key is to understand how to generate uniformly distributed points within a circle. A naive approach might be to generate random x and y coordinates within a bounding square and reject points that fall outside the circle, but this is inefficient (rejection sampling). A more efficient approach uses polar coordinates.
This approach leverages the properties of polar coordinates to generate uniformly distributed points. Here's the breakdown:
Generate a random radius: The radius r
should be chosen such that the probability of selecting any given radius is proportional to its length. This ensures that points are uniformly distributed across all possible areas within the circle. We achieve this by generating a random number between 0 and the square of the radius (r²), then taking the square root. This gives us a radius with the correct probability distribution.
Generate a random angle: The angle θ
(theta) is uniformly chosen between 0 and 2π (360 degrees). This ensures an even distribution around the circle's center.
Convert to Cartesian coordinates: We convert the polar coordinates (r
, θ
) to Cartesian coordinates (x
, y
) using the following formulas:
x = x_center + r * cos(θ)
y = y_center + r * sin(θ)
where x_center
and y_center
are the coordinates of the circle's center.
Time Complexity: The __init__
method has a time complexity of O(1) as it performs constant-time operations. The randPoint
method also has a time complexity of O(1), as it involves a fixed number of arithmetic operations regardless of the circle's size.
Space Complexity: The space complexity is O(1) because the solution uses a fixed amount of memory to store the circle's radius, center coordinates, and generated points. No additional memory scales with input size.
import math
import random
class Solution:
def __init__(self, radius: float, x_center: float, y_center: float):
self.radius = radius
self.x_center = x_center
self.y_center = y_center
def randPoint(self) -> List[float]:
length = math.sqrt(random.uniform(0, self.radius**2)) #Generate random radius
degree = random.uniform(0, 1) * 2 * math.pi #Generate random angle
x = self.x_center + length * math.cos(degree) #Convert to Cartesian coordinates
y = self.y_center + length * math.sin(degree)
return [x, y]
This Python code directly implements the polar coordinate approach described above. The __init__
method initializes the circle's parameters, and randPoint
generates and returns a random point within the circle. The use of random.uniform
ensures uniformly distributed random numbers. The math
module provides trigonometric functions.
Note: Other languages (Java, C++, JavaScript, etc.) would follow a very similar structure, using their respective random number generators and mathematical libraries. The core algorithm remains the same.