1 / 5

Chapter 7

Chapter 7. Monte Carlo Methods to Compute Pi. Circle formed within a square, with unit radius so that square has sides 2*2. Ratio of the area of the circle to the square given by Points within square chosen randomly Score kept of how many points happen to lie within circle

Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7

  2. Monte Carlo Methods to Compute Pi • Circle formed within a square, with unit radius so that square has sides 2*2. Ratio of the area of the circle to the square given by • Points within square chosen randomly • Score kept of how many points happen to lie within circle • Fraction of points within the circle will be pi/4, given a sufficient number of randomly selected samples

  3. Monte Carlo Methods to Compute Pi

  4. Monte Carlo Methods to Compute Pi #include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char* argv[]) { double niter = 100000000; double x,y; int i; int count=0; double z; double pi; srand(time(NULL)); for (i=0; i<niter; ++i) { //get random points x = (double)random()/RAND_MAX; y = (double)random()/RAND_MAX; z = sqrt((x*x)+(y*y)); if (z<=1) { ++count; } } pi = ((double)count/(double)niter)*4.0; printf("Pi: %f\n", pi); }

  5. Monte Carlo Methods to Compute Pi feng.gu@service0:~/csc229> ./pi Pi: 3.141370

More Related