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
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
Visualization
Tap to expand
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code