LeetCode Problem

How to Solve Generate Random Point in a Circle

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.

GhostInterview Help

Need help with Generate Random Point in a Circle without spending extra time grinding it?

GhostInterview can read Generate Random Point in a Circle from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #478Math plus GeometryReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Math plus Geometry
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

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

MetricValue
TimeDepends on the final approach
SpaceDepends 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.

How GhostInterview Helps

  • GhostInterview guides through generating truly uniform points using polar coordinates, avoiding biased radius distributions.
  • It highlights failure modes in rejection sampling and shows when iterative attempts may be inefficient.
  • The tool offers step-by-step verification of point generation to ensure each point adheres to the circle's geometry.

Topic Pages

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.

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.

GhostInterview Solver

Need direct help with Generate Random Point in a Circle instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Generate Random Point in a Circle from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.