Medium
Quiz
#478 Generate Random Point in a Circle
APPROACH
Design an algorithm that samples points uniformly at random from within (or on the boundary of) a circle specified by its radius and center.
Implement the Solution class:
- Solution(double radius, double x_center, double y_center) Stores the circle's radius and center coordinates.
- randPoint() Returns a uniformly random point [x, y] inside or on the boundary of the circle.
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]]
Example 2:
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]
1 of 4
1:00
What is the optimal approach for this problem?