Skip to content
Problem 478 · LeetCode 478(opens in a new tab)

Generate Random Point in a Circle

MediumGeometry

Build a class that picks random points from a filled circle. The constructor Solution(radius, x_center, y_center) receives the circle's radius and the coordinates of its centre.

Each call to randPoint() returns one point [x, y] inside the circle or on its edge. The points must be uniform: every part of the disk with the same area must be equally likely, no matter where it is.

Example 1

Input:  Solution(2.0, 1.0, -1.0), then randPoint() three times
Output: for example [2.0, -1.0], [1.0, 1.0], [0.0, -1.0]
Why:    each point is at most 2 from (1, -1)
2 more examples and the constraints

Example 2

Input:  Solution(1.0, 0.0, 0.0), then randPoint() twice
Output: for example [0.31, -0.64], [-0.9, 0.12]
Why:    any points in the unit disk are valid

Example 3

Input:  Solution(10.0, 5.0, 5.0), then many calls
Output: points spread evenly over the disk
Why:    about a quarter land within 5 of the centre

Constraints

  • 0 < radius ≤ 100,000,000
  • −10,000,000 ≤ x_center, y_center ≤ 10,000,000
  • randPoint is called at most 30,000 times

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Rejection samplingO(1) expected
  2. Angle + square-root distanceO(1)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work per callVerdict
Rejection samplingO(1) expected
Passes; a few retries
Angle + square-root distanceO(1)
Best; one shot per call

Both are constant time; rejection averages 1.27 tries, the polar method always one.

Edge cases to test

  • Points on the edge. A point exactly at distance radius is allowed, so u = 1 or dx² + dy² = 1 is fine.
  • Huge radius, far centre. Radius 10⁸ with centre (10⁷, −10⁷) still works: scale first, then shift, in floating point.
  • Tiny radius. Radius 0.01 works the same way; nothing depends on integer coordinates.
  • Many calls. 30,000 calls must stay spread out: about a quarter within half the radius, a fifth beyond 0.9 of it.

Common mistakes

  • Using a uniform distance instead of its square root, which crowds points near the centre.
  • Drawing x and y uniformly in the square and returning them without the inside check.
  • Forgetting to add the centre, or scaling it by the radius along with the offset.

How to talk through it

  1. 1Clarify. Uniform by area, right? Are points on the edge allowed?
  2. 2Give rejection sampling. Sample the square and retry outside the circle; about 1.27 tries per call.
  3. 3Try polar and name the trap. A uniform distance over-weights the centre, because area grows as d².
  4. 4Fix it with a square root. dist = radius · √u makes the area within each distance come out right.
  5. 5Test out loud. Check every point is inside, and that about 25% fall within half the radius.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?