#478
Generate Random Point in a Circle
expert · 1045 · lc medium +32 · 42.5% accepted · 480 likes · top 24%
Description
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]
Code
1
2
3
4
5
6
7
8
9
10
11
12