From owner-freebsd-questions@FreeBSD.ORG Mon Nov 8 09:01:24 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 165601065672 for ; Mon, 8 Nov 2010 09:01:24 +0000 (UTC) (envelope-from smithi@nimnet.asn.au) Received: from sola.nimnet.asn.au (paqi.nimnet.asn.au [115.70.110.159]) by mx1.freebsd.org (Postfix) with ESMTP id 685858FC1F for ; Mon, 8 Nov 2010 09:01:23 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by sola.nimnet.asn.au (8.14.2/8.14.2) with ESMTP id oA891JIo080054; Mon, 8 Nov 2010 20:01:20 +1100 (EST) (envelope-from smithi@nimnet.asn.au) Date: Mon, 8 Nov 2010 20:01:19 +1100 (EST) From: Ian Smith To: perryh@pluto.rain.com In-Reply-To: <20101106120033.CB14610656D7@hub.freebsd.org> Message-ID: <20101108182717.M66572@sola.nimnet.asn.au> References: <20101106120033.CB14610656D7@hub.freebsd.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Cc: freebsd-questions@freebsd.org, gnrp@physik.tu-berlin.de Subject: Re: how to generate pi in c X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Nov 2010 09:01:24 -0000 In freebsd-questions Digest, Vol 335, Issue 11, Message: 4 On Sat, 06 Nov 2010 01:00:34 -0700 perryh@pluto.rain.com wrote: > Julian Fagir wrote: > > > Does anyone has a "generate-pi.c" source code? > ... > > 1 #include > > 2 #include > > 3 #include > > 4 > > 5 // Change this for a more accurate result. > > 6 long max = 100000000; > > 7 double a, b; > > 8 double pi; > > 9 long counter; > > 10 long i; > > 11 > > 12 int main() { > > 13 for (i = 0; i< max; i++) { > > 14 a = drand48(); > > 15 b = drand48(); > > 16 if (a*a + b*b <= 1) > > 17 counter++; > > 18 } > > 19 pi = 4*counter; Surely that should be 'pi = 4 * counter / max;' otherwise even if the integer counter were only 1 (of 100000000), pi would already be 4 :) > > 20 > > 21 printf("%e\n", pi); > > 22 return(0); > > 23 } > ... > > This approximation is stupid ... Just take 'random' numbers and > > look whether they are in a circle (that's the a*a + b*b <= 1). > > Not stupid, clever. Very clever. I rather doubt it resembles what > the OP had in mind, but it is a brilliant example of what can be > accomplished when one casts aside any perceived need to adopt a > conventional approach. Agreed, quite elegant. Geometry being a bit rusty, I had to sketch it to really see it as simply the ratio of the area of the first quadrant of the unit circle (pi/4) to that of the unit square (1.0), times 4. > > The detail of this approximation heavily depends on the pseudo-rng > > you are using, as does its correctness > > Perhaps it would be useful in a PRNG test suite? > > > (e.g., when your 'rng' always returns 10, pi would be computed to > > be 10) ... > > Bad example. If abs(drand48()) always exceeded (0.5 * sqrt(2.0)), > a*a + b*b would always exceed 1.0, thus counter would never be > incremented and pi would be reported as zero. That'd be one pretty sad PRNG :) but testing variance of the above algorithm's result to best known double pi is likely a useful test. And while a square enclosing a circle, it's hardly squaring the circle: http://en.wikipedia.org/wiki/Squaring_the_circle .. but an interesting read nonetheless for unrequited seekers of pi-foo :) cheers, Ian