[題解]Leetcode 478 Generate Random Point in a Circle 發表於 2021-01-05 分類於 Leetcode題解 閱讀次數: Valine: 文章字數: 684 所需閱讀時間 ≈ 1 分鐘 478 Generate Random Point in a Circle 題目連結 直接Random半徑會出事(可能不夠亂,或是半徑太小),如果random面積之後算半徑才OK。 123456789101112131415161718192021222324class 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(); */