The Monte Carlo method for iOS research is to seek Pi (PI) (

Source: Internet
Author: User

 [excerpt from Baidu Encyclopedia] Monte Carlo method, also known as the statistical simulation method, is the the mid 1940s due to the development of science and technology and the invention of electronic computers, and was proposed by the probability of statistical theory as the guidance of a kind of very important numerical calculation method. means using random numbers (or more common pseudo-random numbers) to solve many computational problems. It corresponds to a deterministic algorithm. The Monte Carlo method is widely used in financial engineering, Macroeconomics, computational physics (such as particle transport calculation, quantum thermodynamic calculation, aerodynamics calculation) and other fields. The last chapter studies the property list in detail needs to understand can look.
The following is a method for solving pi pi using the Monte Carlo method. Algorithm Model:Suppose there is a square with a length of 1 units, and we then use one vertex of the square as the dot, and a 1/4 arc for the radius of 1 units. according to the Circle area formula but: s=πr^2=π. The arc of 1/4 is S/4=Π/4. And the square area is 1. Let's assume a scenario where there are random points that fall within the square, the number of points is N, and the point in the arc is m (how can I tell if a point is inside a circle or an arc?) because the point on the arc is exactly the radius 1, so the center of the circle is less than 1 in the circle, the blue dot in the and red dots in the circle outside), if enough points, will be in the square and arc of the area concerned, that is: 1/4 arc area divided by the square area =π/4=m/n.Then there are: π=4*m/n. If a large number of random numbers are simulated with a computer, the more accurate π is calculated. The following is the C-language implementation of the Monte Carlo method for π.
123456789101112131415161718192021222324252627282930 #include < iostream >#include < Cmath >#include < CTime >#define COUNT 500000//Cycle sample Countusingnamespace  STD;    //Determine if the point is within the 1/4 Circle rangeBOOL  incircle(Double  x,Double  y)  {      if((x *x +y * y)<= 1)           return  true ;      return  false ;  }    void  Main()  {      Double  x,y;      int  Num= 0;      int  I;         Srand((unsigned) Time(NULL));       for(I= 0;I< COUNT;I++ )      {          x= Rand()* 1.0/ Rand_max;//generates a random number between 0-1        y= Rand()* 1.0/ Rand_max;          if(incircle(x,y))              Num++ ;      }      cout<< "PI:"<< (num *4.0 )/ COUNT<< Endl;  }  

Related Article

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.