From owner-freebsd-questions@FreeBSD.ORG Sat Nov 6 08:12:00 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 C03941065673 for ; Sat, 6 Nov 2010 08:12:00 +0000 (UTC) (envelope-from perryh@pluto.rain.com) Received: from agora.rdrop.com (agora.rdrop.com [IPv6:2607:f678:1010::34]) by mx1.freebsd.org (Postfix) with ESMTP id 84CD28FC17 for ; Sat, 6 Nov 2010 08:12:00 +0000 (UTC) Received: from agora.rdrop.com (66@localhost [127.0.0.1]) by agora.rdrop.com (8.13.1/8.12.7) with ESMTP id oA68Bxks095843 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sat, 6 Nov 2010 01:11:59 -0700 (PDT) (envelope-from perryh@pluto.rain.com) Received: (from uucp@localhost) by agora.rdrop.com (8.13.1/8.12.9/Submit) with UUCP id oA68BwrX095842; Sat, 6 Nov 2010 01:11:58 -0700 (PDT) Received: from fbsd61 by pluto.rain.com (4.1/SMI-4.1-pluto-M2060407) id AA18258; Sat, 6 Nov 10 00:00:29 PST Date: Sat, 06 Nov 2010 01:00:34 -0700 From: perryh@pluto.rain.com To: gnrp@physik.tu-berlin.de Message-Id: <4cd50b22.r80gL0SMmLLFy03t%perryh@pluto.rain.com> References: <20101105195019.4462c479@adolfputzen> In-Reply-To: <20101105195019.4462c479@adolfputzen> User-Agent: nail 11.25 7/29/05 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org 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: Sat, 06 Nov 2010 08:12:00 -0000 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; > 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. > 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.