LeetCode Problem Workspace

Generate Random Point in a Circle

Generate Random Point in a Circle requires creating a uniform random point inside a circle using math and geometry principles efficiently.

category

4

Topics

code_blocks

1

Code langs

hub

3

Related

Practice Focus

Medium · Math plus Geometry

bolt

Answer-first summary

Generate Random Point in a Circle requires creating a uniform random point inside a circle using math and geometry principles efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus Geometry

Try AiBox Copilotarrow_forward

This problem tests understanding of geometry and probability, requiring a solution that produces uniform random points inside a circle. The challenge lies in converting polar coordinates to Cartesian while avoiding biased distributions. Efficient approaches combine radius scaling, angle generation, and sometimes rejection sampling to guarantee true uniformity within the circle.

Problem Statement

Given a circle defined by its radius and center coordinates, implement a function that returns a point randomly located inside the circle with uniform probability. The point should be represented as a pair of floating-point coordinates [x, y].

Implement a class Solution with a constructor Solution(radius, x_center, y_center) and a method randPoint() which returns a new random point inside the circle each time it is called. Ensure the random points follow uniform distribution and adhere to the constraints on radius and center values.

Examples

Example 1

Input: See original problem statement.

Output: See original problem statement.

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
  • At most 3 * 104 calls will be made to randPoint.

Solution Approach

Use Polar Coordinates for Uniform Sampling

Generate a random angle θ uniformly from 0 to 2π and a radius r scaled by the square root of a uniform random number. Convert to Cartesian coordinates using x = x_center + r * cos(θ) and y = y_center + r * sin(θ). This approach ensures uniform coverage across the circle area.

Apply Rejection Sampling

Alternatively, sample x and y within the bounding square of the circle and reject points outside the circle. Repeat until a valid point is found. This method emphasizes understanding of geometry boundaries but may require multiple iterations for large radii.

Trade-offs Between Efficiency and Simplicity

Using polar coordinates is efficient and avoids wasted computations, while rejection sampling is simpler but can be inefficient. Choosing the method depends on the need for performance versus straightforward implementation for interview contexts.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity depends on the chosen method: polar coordinate sampling is O(1) per call, while rejection sampling may vary depending on circle density in the bounding square. Space complexity is O(1) as only coordinates and random values are stored.

What Interviewers Usually Probe

  • Expect questions about uniform distribution and radius scaling for correctness.
  • Interviewers often probe handling of boundary cases near the circle edge.
  • Be prepared to justify polar vs Cartesian or rejection sampling trade-offs.

Common Pitfalls or Variants

Common pitfalls

  • Generating the radius uniformly without square root leads to biased point distribution towards the center.
  • Confusing the circle's bounding square limits with valid circle coordinates.
  • Failing to convert polar coordinates correctly to Cartesian coordinates.

Follow-up variants

  • Generate a random point inside an ellipse instead of a circle.
  • Generate multiple random points efficiently without repeated function calls.
  • Modify to generate points only in a specific quadrant of the circle.

FAQ

How do you ensure uniform random points in a circle?

Use polar coordinates with the radius scaled by the square root of a uniform random number and angle uniformly between 0 and 2π.

Why does simple uniform radius sampling fail?

Sampling the radius directly leads to higher density near the center because area scales with r^2, not linearly.

Is rejection sampling always recommended?

Rejection sampling is simple but can be inefficient, especially for large circles relative to bounding squares.

How is this problem pattern classified?

It follows a math plus geometry pattern with a focus on uniform random point generation inside geometric shapes.

Can GhostInterview help verify points are uniform?

Yes, it can check multiple generated points against expected uniform distributions to ensure the implementation is correct.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
7
8
9
10
11
12
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))
        degree = random.uniform(0, 1) * 2 * math.pi
        x = self.x_center + length * math.cos(degree)
        y = self.y_center + length * math.sin(degree)
        return [x, y]
Generate Random Point in a Circle Solution: Math plus Geometry | LeetCode #478 Medium