/*Monte Carlo's claim for Pi illustrates that Monte Carlo is the capital of the Kingdom of Morocco, a country located in France and Italy, famous for its gambling. Monte Carlo's basic principle is to solve the problem with the chaotic number of the area formula, this probability to solve the problem with the means of gambling, although the accuracy of some doubts, but its problem-solving thinking direction is a way worth learning. The solution of Monte Carlo method is applicable to the area-related problems, such as the calculation of the PI value or ellipse area, here is how to find the pi value; Suppose there is a circle radius of 1, so One-fourth Circle area is pi, and including this One-fourth Circle Square area is 1, as shown:!!! The lack of a picture here if the random in the square to shoot the flying mark (point), then some of these flying mark (points) will fall in the One-fourth circle, assuming that the projection of the flying mark (point) has n points, in the circle of the Flying mark (point) has a C point, then proportional to calculate, will be the final formula. As to how to judge the resulting points fall in the circle, it is very simple, so that the number of chaos generated X and Y two values, if the x^2+y^2 equals 1 is falling in the circle. */#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN 50000intMainvoid){ intI, sum =0; Doublex, y; Srand (Time (NULL)); for(i =1; i < N; i++) {x= (Double) rand ()/Rand_max; Y= (Double) rand ()/Rand_max; if((x * x + y * y) <1) {sum++; }} printf ("PI =%f \ n", (Double)4*sum/N); return 0;}
The results are as follows:
"Monte Carlo method to seek PI"