Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: notesfiles Path: utzoo!linus!decvax!tektronix!hplabs!hp-pcd!hpfcla!jad From: jad@hpfcla.UUCP (jad) Newsgroups: net.sources Subject: Re: Orphaned Response Message-ID: <50600006@hpfclo.UUCP> Date: Tue, 13-Aug-85 17:59:00 EDT Article-I.D.: hpfclo.50600006 Posted: Tue Aug 13 17:59:00 1985 Date-Received: Mon, 19-Aug-85 23:26:49 EDT References: <187@ski.UUCP> Organization: Hewlett-Packard - Fort Collins, CO Lines: 98 Nf-ID: #R:ski:187:hpfclo:50600006:37777777600:2181 Nf-From: hpfclo!jad Aug 13 13:59:00 1985 > /***** hpfclo:net.sources / uiucuxc!grayson / 7:21 pm Aug 1, 1985*/ > > Here is the fastest way ever, and it is not a series. It is called > the Gauss-Legendre method, and was discovered independently by Salamin and > Brent. It is based on the arithmetic-geometric mean, which is: So, now that we have a nice fast way to find pi, how about something fun? This one estimates the value of pi by placing points on in square area, and seeing how many fall in a circle. This is probably about the slowest (and least accurate) way to find pi, but it's far more amusing. -- jad -- John A. Dilley, FSD Fort Collins, CO ARPA: terrapin@Purdue.EDU UUCP: {ihnp4}! hpfcla!jad PHONE: (303)226-3800 x4166 /* **-- Estimate pi by randomly throwing things at a circle ** ** programmer: John Dilley (hpfclo:jad) ** ** creation date: Tue Jul 30 10:02 ** ** Randomly tosses points into an arena with a circle ** defined ... determining how many fell into the circular ** area allows you to estimate the value of pi. Not real ** accurate, of course! ** ** CopyWrong (c) 198? ** Permission granted for selling this thing for as much ** as you can get for it, and you don't even have to claim ** you got it from me. In fact, I'd rather you didn't ... */ main() { register int i=0, n=0, j; double x, y; printf("\014Set random number\n"); while (1) { for (j=0; j<100; j++) { x=RandReal(); y=RandReal(); if (x*x+y*y < 1.0) i++; } n += 100; printf("%d\t%d\t%f\n", i,n,4.0*i/n); } } /* ** -- Random number generator and clock based randomizer ** ** programmer: John Dilley (mordred:jad) ** ** creation date: Thu Feb 7 14:19 */ # include struct timeval time; struct timezone zone; static double seed=0.0; /* ** Randomize() -- seed the random number generator */ Randomize() { gettimeofday( &time, &zone ); seed = (double) ((time.tv_usec&017777700)>>6) / (double)(01777); } /* ** RandReal() -- return a random real number in [0,1) */ RandReal() { if (seed == 0.0) Randomize(); seed = seed*9821 + 0.211327; seed -= (int)seed; return seed; }