"Monte Carlo method to seek PI"

Source: Internet
Author: User

/*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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.