Leetcode Problem 478. Generate Random Point in a Circle

478. Generate Random Point in a Circle

Leetcode Solutions

Inverse Transform Sampling

  1. Generate a uniform random number u in the range [0, 1).
  2. Calculate the distance D from the origin using the inverse CDF: D = sqrt(u).
  3. Generate a uniform random angle theta in the range [0, 2*PI).
  4. Convert polar coordinates (D, theta) to Cartesian coordinates to get the point (x, y):
    • x = D * cos(theta)
    • y = D * sin(theta)
  5. Adjust the point (x, y) by the circle's center (x_center, y_center):
    • x += x_center
    • y += y_center
  6. Return the point (x, y) as the random point inside the circle.
UML Thumbnail

Rejection Sampling

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...