Orphaned Response

jad jad at hpfcla.UUCP
Wed Aug 14 07:59:00 AEST 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 at Purdue.EDU
UUCP:			      {ihnp4}! hpfcla!jad
PHONE:			      (303)226-3800 x4166



/*
**	<rand_pi.c> --	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);
	}
}

/*
**	<rand.c> --	Random number generator and clock based randomizer
**
**	programmer:	John Dilley	(mordred:jad)
**
**	creation date:	Thu Feb  7 14:19
*/

# include	<sys/time.h>

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;
}



More information about the Comp.sources.unix mailing list