Generate Random Point in a Circle - Problem

Imagine you're creating a game where players need to click random locations inside a circular target. You need to ensure these points are uniformly distributed across the entire circle area - not clustered toward the center!

Given a circle defined by its radius and center coordinates (x_center, y_center), your task is to implement a system that generates random points inside this circle with perfect uniform distribution.

Challenge: Simply using random radius and angle won't work - this creates more points near the center because inner rings have less area than outer rings!

Your Task:

  • Implement Solution(radius, x_center, y_center) constructor
  • Implement randPoint() that returns [x, y] coordinates
  • Points on the circumference are considered inside the circle
  • Ensure uniform distribution across the entire circle area

Input & Output

example_1.py β€” Basic Circle
$ Input: Solution(1.0, 0.0, 0.0)\nrandPoint() called multiple times
β€Ί Output: Possible outputs: [-0.123, 0.456], [0.789, -0.234], [0.0, 0.999]
πŸ’‘ Note: For a unit circle centered at origin, all returned points should satisfy xΒ² + yΒ² ≀ 1.0
example_2.py β€” Offset Center
$ Input: Solution(2.0, 3.0, 4.0)\nrandPoint() called multiple times
β€Ί Output: Possible outputs: [4.567, 5.123], [1.234, 2.789], [3.0, 6.0]
πŸ’‘ Note: For radius 2 centered at (3,4), points should satisfy (x-3)Β² + (y-4)Β² ≀ 4
example_3.py β€” Edge Case Small Radius
$ Input: Solution(0.01, 10.0, 10.0)\nrandPoint() called multiple times
β€Ί Output: Possible outputs: [10.005, 9.997], [9.999, 10.008], [10.001, 9.995]
πŸ’‘ Note: Even with very small radius, points should be uniformly distributed within the tiny circle

Visualization

Tap to expand
Naive vs Optimal Distribution❌ Naive: Random RadiusClustered near centerβœ… Optimal: √(Random) RadiusUniformly distributedKey Mathematical InsightRing area grows as Ο€(rβ‚‚Β² - r₁²) - outer rings need higher selection probabilityUsing √(random) for radius gives uniform distribution across the entire circle area
Understanding the Visualization
1
The Problem
Simple random radius creates more points near center (smaller rings have less area)
2
The Solution
Square root transformation: r = R√(random) compensates for area differences
3
Perfect Distribution
Every equal-sized region now has equal probability of containing a point
Key Takeaway
🎯 Key Insight: Area scales with r², so radius must scale with √(random) for uniform distribution!

Time & Space Complexity

Time Complexity
⏱️
O(1)

Fixed number of operations: 2 random numbers, 1 sqrt, 1 cos, 1 sin

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only stores the circle parameters and temporary variables

n
2n
βœ“ Linear Space

Constraints

  • 0 < radius ≀ 108
  • -107 ≀ x_center, y_center ≀ 107
  • At most 3 Γ— 104 calls will be made to randPoint
  • Points on the circumference are considered inside the circle
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 18
89.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen