> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Generate Random Point in a Circle

> Tested Python solution for LeetCode 478 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 478, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry), Rejection Sampling, Randomized. [View on LeetCode](https://leetcode.com/problems/generate-random-point-in-a-circle/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 478   # by problem number
lcpy gen -s generate_random_point_in_a_circle   # by problem name
```

## Problem

Given the radius and the position of the center of a circle, implement the function `randPoint` which generates a uniform random point inside the circle.

Implement the `Solution` class:

* `Solution(double radius, double x_center, double y_center)` initializes the object with the radius of the circle `radius` and the position of the center `(x_center, y_center)`.
* `randPoint()` returns a random point inside the circle. A point on the circumference of the circle is considered to be in the circle. The answer is returned as an array `[x, y]`.

### Examples

```
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 \<= 10^8
* -10^7 \<= x\_center, y\_center \<= 10^7
* At most 3 \* 10^4 calls will be made to randPoint.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/generate_random_point_in_a_circle/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/generate_random_point_in_a_circle/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import math
import random


class Solution:
    # Time: O(1) per rand_point call, O(1) init
    # Space: O(1)

    def __init__(self, radius: float, x_center: float, y_center: float) -> None:
        self.radius = radius
        self.x_center = x_center
        self.y_center = y_center

    def rand_point(self) -> list[float]:
        # Sampling radius as sqrt(u) * R makes the point density uniform per
        # unit area: a uniform angle sweeps equal area only at equal radii, so
        # the radial CDF r^2/R^2 must be inverted with sqrt(u).
        length = math.sqrt(random.random()) * self.radius
        angle = random.uniform(0, 2 * math.pi)
        return [
            self.x_center + length * math.cos(angle),
            self.y_center + length * math.sin(angle),
        ]
```

## Complexity

| Time | Space |
| - | - |
| O(1) per rand\_point call, O(1) init | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.