两个问题:
1.随机取半径和角度并不是真正的随机
2.期望的计算
class Solution { public: Solution(double radius, double x_center, double y_center) { rad = radius; x = x_center; y = y_center; } vector<double> randPoint() { const double PI = 3.141592653; //rad代表半径 2*PI代表360度==2PI 故对角度和长度都取随机值,并通过开根号的方式控制远离圆心的概率增加,来达到整体的均衡【原本是靠近圆心的点少,所以最后分布会更接近圆心】 double nr = sqrt(rand() / double(RAND_MAX)) * rad; double alpha = rand() / double(RAND_MAX) * 2 * PI; double nx = x + nr * cos(alpha); double ny = y + nr * sin(alpha); return {nx, ny}; } private: double rad; double x, y; }; /** * 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(); */
愿为天下目,萃聚六路华