Skip to content

Practice Generate Random Point in a Circle

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#478
LeetCode Problem

Generate Random Point in a Circle

Problem Statement

Solution(radius,x_center,y_center) describes a circle. randPoint() returns [x,y] uniformly by area inside it, including its boundary. Practice approach: Draw a uniform angle and an independent uniform U in [0,1), then use radius * sqrt(U) as the distance from the center. The square root compensates for the larger area at greater radii. Sampling radius uniformly would overconcentrate points near the center. Rejection sampling from the surrounding square is another valid construction. Check that squared distance never exceeds radius squared.

Source: Generate Random Point in a Circle on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["Solution","randPoint","randPoint"]
[[2,3,4],[],[]]
Output
[null,[3.5,4.5],[2.2,3.4]]
Explanation

These are illustrative possible points, not fixed outputs. Their squared distances from (3,4) are 0.5 and 1, both below radius squared 4.

Constraints

0 < radius <= 10^8
-10^7 <= x_center, y_center <= 10^7
At most 3 * 10^4 randPoint calls.

Related LeetCode Problems

Practice more problems similar to Generate Random Point in a Circle.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.