The first thought of most people getting this question is
Select a random point in the square of the X axis [-] and y axis. Then, determine whether the vertex is in the circle (by calculating the distance from this vertex to the center of the circle ). If it is in the circle, this point is the result. If it is not in the circle, it is re-selected until it is found. The square area is 4, and the circle area is pi. Therefore, the probability of random points in the Square within the circle is PI/4.
If you are familiar with machine learning algorithms, this method is calledReject sampling.It is to use a probability distribution that is easy to generate (this is the uniform distribution of squares) to simulate the probability that is not easy to generate.
To simulate uniform distribution on the unit circle, it is necessary to simulate a probability distribution.FRandom Variable, whereFYes.
We select g to be evenly distributed on a square with a center at the origin and a side length of 2.
Note that the probability of falling into the circle is PI/4 (the area of the circle is pi, and the area of the square is 4). The probability of not falling into the circle is 1-PI/4, the probability that K tests do not fall into the circle is (1-PI/4) ^ K. When k = 20, the result is similar. 00000000000004. When k = 50, it is approximately 10 ^ {-34}. It takes some time.
Another method is
Select an angle randomly from [* PI), corresponding to a radius in the circle, and then select a point on the radius. However, the points on the radius cannot be evenly selected. The selected probability should be proportional to the length from the center of the circle, so that the random points are evenly distributed within the circle.
Http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly