# 478. Generate Random Point in a Circle¶

• Time:
• Space:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public: Solution(double radius, double x_center, double y_center) : radius(radius), x_center(x_center), y_center(y_center) {} vector randPoint() { const double length = sqrt(distribution(generator)) * radius; const double degree = distribution(generator) * 2 * M_PI; const double x = x_center + length * cos(degree); const double y = y_center + length * sin(degree); return {x, y}; } private: const double radius; const double x_center; const double y_center; default_random_engine generator; uniform_real_distribution distribution = uniform_real_distribution(0.0, 1.0); }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public Solution(double radius, double x_center, double y_center) { this.radius = radius; this.x_center = x_center; this.y_center = y_center; } public double[] randPoint() { final double length = Math.sqrt(Math.random()) * radius; final double degree = Math.random() * 2 * Math.PI; final double x = x_center + length * Math.cos(degree); final double y = y_center + length * Math.sin(degree); return new double[] {x, y}; } private double radius; private double x_center; private double y_center; } 
  1 2 3 4 5 6 7 8 9 10 11 12 class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.radius = radius self.x_center = x_center self.y_center = y_center def randPoint(self) -> List[float]: length = sqrt(random.uniform(0, 1)) * self.radius degree = random.uniform(0, 1) * 2 * math.pi x = self.x_center + length * math.cos(degree) y = self.y_center + length * math.sin(degree) return [x, y]