class Solution {
private:
double c_radius;
double xcenter;
double ycenter;
public:
Solution(double radius, double x_center, double y_center) {
c_radius = radius;
xcenter = x_center;
ycenter = y_center;
}
vector<double> randPoint() {
double randomradius= (sqrt((double)rand()) / RAND_MAX) * c_radius;
double randomangle= ((double)rand() / RAND_MAX) * 360.0;
double yp= sin(randomangle)*randomradius;
double xp= cos(randomangle)*randomradius;
std::vector<double> point= {xp+xcenter, yp+ycenter};
return point;
}
};
Hi everyone, I was doing a Leet Code question about finding a random point within a circle. It all goes well until following test: Inputs: [[0.01,-73839.1,-3289891.3]
It is essentially generating a random point within a circle however my result seems to be rounded off and I am not sure why.
My Output after generating point:
[null,[-73839.10**000**,-3289891.30**000**],[-73839.10**000**,-3289891.30000],[-73839.10000,-3289891.30000],[-73839.10000,-3289891.30000],[-73839.10000,-3289891.30000]...
Expected Output
[null,[-73839.10**006**,-3289891.30**228**],[-73839.10**541**,-3289891.30660],[-73839.10634,-3289891.30124],[-73839.10256,-3289891.30684],[-73839.09825,-3289891.29962]...
Now my question is where is the error? Is there a fault in my math? The results are close but not close enough. I cannot seem to pinpoint where this error occurs. Any help is greatly appreciated.