Problem Description
Design a Solution class that, given a circle defined by its radius and center, provides a method randPoint() which generates a uniformly random point within the circle. Points on the circle's circumference are considered inside.
Key Insights
- The uniform distribution is achieved by careful sampling in polar coordinates.
- Directly choosing a random radius uniformly does not yield a uniform distribution in the circle. Instead, generate the square root of a uniformly distributed value.
- Compute the coordinates using: x = x_center + r * cos(theta), y = y_center + r * sin(theta).
Space and Time Complexity
Time Complexity: O(1) per call to randPoint() Space Complexity: O(1)
Solution
The solution leverages polar coordinates to ensure uniform randomness within the circle. The key steps are:
- Generate a random angle theta between 0 and 2π.
- Generate a random number between 0 and 1, take its square root, and multiply by the circle's radius to obtain the random distance (r). This ensures that the probability distribution is uniform over the circle.
- Convert the polar coordinates (r, theta) to Cartesian coordinates using cosine and sine functions.
- Offset the computed coordinates by the center of the circle (x_center, y_center).
This approach guarantees both uniform distribution and an efficient O(1) solution per point generation.