0%

[題解]Leetcode 478 Generate Random Point in a Circle

478 Generate Random Point in a Circle

題目連結

直接Random半徑會出事(可能不夠亂,或是半徑太小),如果random面積之後算半徑才OK。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
double R,X,Y;
Solution(double radius, double x_center, double y_center) {
R = radius;X = x_center;Y = y_center;
srand(time(NULL));
}

vector<double> randPoint() {
double Area = rand() * R * R * M_PI / (RAND_MAX + 1.0);
double r = sqrt(Area / M_PI);
double theta = 2.0 * M_PI * rand() / (RAND_MAX + 1.0);
vector<double> ans;
ans.push_back(X + r * cos(theta));
ans.push_back(Y + r * sin(theta));
return ans;
}
};

/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(radius, x_center, y_center);
* vector<double> param_1 = obj->randPoint();
*/